Projet

Général

Profil

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

root / drupal7 / modules / simpletest / tests / pager.test @ 27e02aed

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
   * Tests theme_pager() when an empty quantity is passed.
60
   */
61
  public function testThemePagerQuantityNotSet() {
62
    $variables = array(
63
      'element' => 0,
64
      'parameters' => array(),
65
      'quantity' => '',
66
      'tags' => '',
67
    );
68
    pager_default_initialize(100, 10);
69
    $rendered_output = theme_pager($variables);
70
    $this->assertNotIdentical(stripos($rendered_output, 'next'), FALSE);
71
    $this->assertNotIdentical(stripos($rendered_output, 'last'), FALSE);
72
  }
73

    
74
  /**
75
   * Asserts pager items and links.
76
   *
77
   * @param int $current_page
78
   *   The current pager page the internal browser is on.
79
   */
80
  protected function assertPagerItems($current_page) {
81
    $elements = $this->xpath('//ul[@class=:class]/li', array(':class' => 'pager'));
82
    $this->assertTrue(!empty($elements), 'Pager found.');
83

    
84
    // Make current page 1-based.
85
    $current_page++;
86

    
87
    // Extract first/previous and next/last items.
88
    // first/previous only exist, if the current page is not the first.
89
    if ($current_page > 1) {
90
      $first = array_shift($elements);
91
      $previous = array_shift($elements);
92
    }
93
    // next/last always exist, unless the current page is the last.
94
    if ($current_page != count($elements)) {
95
      $last = array_pop($elements);
96
      $next = array_pop($elements);
97
    }
98

    
99
    // Verify items and links to pages.
100
    foreach ($elements as $page => $element) {
101
      // Make item/page index 1-based.
102
      $page++;
103
      if ($current_page == $page) {
104
        $this->assertClass($element, 'pager-current', 'Item for current page has .pager-current class.');
105
        $this->assertFalse(isset($element->a), 'Item for current page has no link.');
106
      }
107
      else {
108
        $this->assertNoClass($element, 'pager-current', "Item for page $page has no .pager-current class.");
109
        $this->assertClass($element, 'pager-item', "Item for page $page has .pager-item class.");
110
        $this->assertTrue($element->a, "Link to page $page found.");
111
        $this->assertNoClass($element->a, 'active', "Link to page $page is not active.");
112
      }
113
      unset($elements[--$page]);
114
    }
115
    // Verify that no other items remain untested.
116
    $this->assertTrue(empty($elements), 'All expected items found.');
117

    
118
    // Verify first/previous and next/last items and links.
119
    if (isset($first)) {
120
      $this->assertClass($first, 'pager-first', 'Item for first page has .pager-first class.');
121
      $this->assertTrue($first->a, 'Link to first page found.');
122
      $this->assertNoClass($first->a, 'active', 'Link to first page is not active.');
123
    }
124
    if (isset($previous)) {
125
      $this->assertClass($previous, 'pager-previous', 'Item for first page has .pager-previous class.');
126
      $this->assertTrue($previous->a, 'Link to previous page found.');
127
      $this->assertNoClass($previous->a, 'active', 'Link to previous page is not active.');
128
    }
129
    if (isset($next)) {
130
      $this->assertClass($next, 'pager-next', 'Item for next page has .pager-next class.');
131
      $this->assertTrue($next->a, 'Link to next page found.');
132
      $this->assertNoClass($next->a, 'active', 'Link to next page is not active.');
133
    }
134
    if (isset($last)) {
135
      $this->assertClass($last, 'pager-last', 'Item for last page has .pager-last class.');
136
      $this->assertTrue($last->a, 'Link to last page found.');
137
      $this->assertNoClass($last->a, 'active', 'Link to last page is not active.');
138
    }
139
  }
140

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

    
158
  /**
159
   * Asserts that an element does not have a given class.
160
   *
161
   * @param SimpleXMLElement $element
162
   *   The element to test.
163
   * @param string $class
164
   *   The class to assert.
165
   * @param string $message
166
   *   (optional) A verbose message to output.
167
   */
168
  protected function assertNoClass(SimpleXMLElement $element, $class, $message = NULL) {
169
    if (!isset($message)) {
170
      $message = "Class .$class not found.";
171
    }
172
    $this->assertTrue(strpos($element['class'], $class) === FALSE, $message);
173
  }
174
}