Projet

Général

Profil

Paste
Télécharger (5,36 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / simpletest / tests / pager.test @ 76597ebf

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for pager functionality.
6
 */
7

    
8
/**
9
 * Tests pager functionality.
10
 */
11
class PagerFunctionalWebTestCase extends DrupalWebTestCase {
12
  protected $profile = 'testing';
13

    
14
  public static function getInfo() {
15
    return array(
16
      'name' => 'Pager functionality',
17
      'description' => 'Tests pager functionality.',
18
      'group' => 'Pager',
19
    );
20
  }
21

    
22
  function setUp() {
23
    parent::setUp(array('dblog'));
24

    
25
    // Insert 300 log messages.
26
    for ($i = 0; $i < 300; $i++) {
27
      watchdog('pager_test', $this->randomString(), NULL, WATCHDOG_DEBUG);
28
    }
29

    
30
    $this->admin_user = $this->drupalCreateUser(array(
31
      'access site reports',
32
    ));
33
    $this->drupalLogin($this->admin_user);
34
  }
35

    
36
  /**
37
   * Tests markup and CSS classes of pager links.
38
   */
39
  function testActiveClass() {
40
    // Verify first page.
41
    $this->drupalGet('admin/reports/dblog');
42
    $current_page = 0;
43
    $this->assertPagerItems($current_page);
44

    
45
    // Verify any page but first/last.
46
    $current_page++;
47
    $this->drupalGet('admin/reports/dblog', array('query' => array('page' => $current_page)));
48
    $this->assertPagerItems($current_page);
49

    
50
    // Verify last page.
51
    $elements = $this->xpath('//li[contains(@class, :class)]/a', array(':class' => 'pager-last'));
52
    preg_match('@page=(\d+)@', $elements[0]['href'], $matches);
53
    $current_page = (int) $matches[1];
54
    $this->drupalGet($GLOBALS['base_root'] . $elements[0]['href'], array('external' => TRUE));
55
    $this->assertPagerItems($current_page);
56
  }
57

    
58
  /**
59
   * Asserts pager items and links.
60
   *
61
   * @param int $current_page
62
   *   The current pager page the internal browser is on.
63
   */
64
  protected function assertPagerItems($current_page) {
65
    $elements = $this->xpath('//ul[@class=:class]/li', array(':class' => 'pager'));
66
    $this->assertTrue(!empty($elements), 'Pager found.');
67

    
68
    // Make current page 1-based.
69
    $current_page++;
70

    
71
    // Extract first/previous and next/last items.
72
    // first/previous only exist, if the current page is not the first.
73
    if ($current_page > 1) {
74
      $first = array_shift($elements);
75
      $previous = array_shift($elements);
76
    }
77
    // next/last always exist, unless the current page is the last.
78
    if ($current_page != count($elements)) {
79
      $last = array_pop($elements);
80
      $next = array_pop($elements);
81
    }
82

    
83
    // Verify items and links to pages.
84
    foreach ($elements as $page => $element) {
85
      // Make item/page index 1-based.
86
      $page++;
87
      if ($current_page == $page) {
88
        $this->assertClass($element, 'pager-current', 'Item for current page has .pager-current class.');
89
        $this->assertFalse(isset($element->a), 'Item for current page has no link.');
90
      }
91
      else {
92
        $this->assertNoClass($element, 'pager-current', "Item for page $page has no .pager-current class.");
93
        $this->assertClass($element, 'pager-item', "Item for page $page has .pager-item class.");
94
        $this->assertTrue($element->a, "Link to page $page found.");
95
        $this->assertNoClass($element->a, 'active', "Link to page $page is not active.");
96
      }
97
      unset($elements[--$page]);
98
    }
99
    // Verify that no other items remain untested.
100
    $this->assertTrue(empty($elements), 'All expected items found.');
101

    
102
    // Verify first/previous and next/last items and links.
103
    if (isset($first)) {
104
      $this->assertClass($first, 'pager-first', 'Item for first page has .pager-first class.');
105
      $this->assertTrue($first->a, 'Link to first page found.');
106
      $this->assertNoClass($first->a, 'active', 'Link to first page is not active.');
107
    }
108
    if (isset($previous)) {
109
      $this->assertClass($previous, 'pager-previous', 'Item for first page has .pager-previous class.');
110
      $this->assertTrue($previous->a, 'Link to previous page found.');
111
      $this->assertNoClass($previous->a, 'active', 'Link to previous page is not active.');
112
    }
113
    if (isset($next)) {
114
      $this->assertClass($next, 'pager-next', 'Item for next page has .pager-next class.');
115
      $this->assertTrue($next->a, 'Link to next page found.');
116
      $this->assertNoClass($next->a, 'active', 'Link to next page is not active.');
117
    }
118
    if (isset($last)) {
119
      $this->assertClass($last, 'pager-last', 'Item for last page has .pager-last class.');
120
      $this->assertTrue($last->a, 'Link to last page found.');
121
      $this->assertNoClass($last->a, 'active', 'Link to last page is not active.');
122
    }
123
  }
124

    
125
  /**
126
   * Asserts that an element has a given class.
127
   *
128
   * @param SimpleXMLElement $element
129
   *   The element to test.
130
   * @param string $class
131
   *   The class to assert.
132
   * @param string $message
133
   *   (optional) A verbose message to output.
134
   */
135
  protected function assertClass(SimpleXMLElement $element, $class, $message = NULL) {
136
    if (!isset($message)) {
137
      $message = "Class .$class found.";
138
    }
139
    $this->assertTrue(strpos($element['class'], $class) !== FALSE, $message);
140
  }
141

    
142
  /**
143
   * Asserts that an element does not have a given class.
144
   *
145
   * @param SimpleXMLElement $element
146
   *   The element to test.
147
   * @param string $class
148
   *   The class to assert.
149
   * @param string $message
150
   *   (optional) A verbose message to output.
151
   */
152
  protected function assertNoClass(SimpleXMLElement $element, $class, $message = NULL) {
153
    if (!isset($message)) {
154
      $message = "Class .$class not found.";
155
    }
156
    $this->assertTrue(strpos($element['class'], $class) === FALSE, $message);
157
  }
158
}
159