pager.test 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * @file
  4. * Tests for pager functionality.
  5. */
  6. /**
  7. * Tests pager functionality.
  8. */
  9. class PagerFunctionalWebTestCase extends DrupalWebTestCase {
  10. protected $profile = 'testing';
  11. public static function getInfo() {
  12. return array(
  13. 'name' => 'Pager functionality',
  14. 'description' => 'Tests pager functionality.',
  15. 'group' => 'Pager',
  16. );
  17. }
  18. function setUp() {
  19. parent::setUp(array('dblog'));
  20. // Insert 300 log messages.
  21. for ($i = 0; $i < 300; $i++) {
  22. watchdog('pager_test', $this->randomString(), NULL, WATCHDOG_DEBUG);
  23. }
  24. $this->admin_user = $this->drupalCreateUser(array(
  25. 'access site reports',
  26. ));
  27. $this->drupalLogin($this->admin_user);
  28. }
  29. /**
  30. * Tests markup and CSS classes of pager links.
  31. */
  32. function testActiveClass() {
  33. // Verify first page.
  34. $this->drupalGet('admin/reports/dblog');
  35. $current_page = 0;
  36. $this->assertPagerItems($current_page);
  37. // Verify any page but first/last.
  38. $current_page++;
  39. $this->drupalGet('admin/reports/dblog', array('query' => array('page' => $current_page)));
  40. $this->assertPagerItems($current_page);
  41. // Verify last page.
  42. $elements = $this->xpath('//li[contains(@class, :class)]/a', array(':class' => 'pager-last'));
  43. preg_match('@page=(\d+)@', $elements[0]['href'], $matches);
  44. $current_page = (int) $matches[1];
  45. $this->drupalGet($GLOBALS['base_root'] . $elements[0]['href'], array('external' => TRUE));
  46. $this->assertPagerItems($current_page);
  47. }
  48. /**
  49. * Tests theme_pager() when an empty quantity is passed.
  50. */
  51. public function testThemePagerQuantityNotSet() {
  52. $variables = array(
  53. 'element' => 0,
  54. 'parameters' => array(),
  55. 'quantity' => '',
  56. 'tags' => '',
  57. );
  58. pager_default_initialize(100, 10);
  59. $rendered_output = theme_pager($variables);
  60. $this->assertNotIdentical(stripos($rendered_output, 'next'), FALSE);
  61. $this->assertNotIdentical(stripos($rendered_output, 'last'), FALSE);
  62. }
  63. /**
  64. * Asserts pager items and links.
  65. *
  66. * @param int $current_page
  67. * The current pager page the internal browser is on.
  68. */
  69. protected function assertPagerItems($current_page) {
  70. $elements = $this->xpath('//ul[@class=:class]/li', array(':class' => 'pager'));
  71. $this->assertTrue(!empty($elements), 'Pager found.');
  72. // Make current page 1-based.
  73. $current_page++;
  74. // Extract first/previous and next/last items.
  75. // first/previous only exist, if the current page is not the first.
  76. if ($current_page > 1) {
  77. $first = array_shift($elements);
  78. $previous = array_shift($elements);
  79. }
  80. // next/last always exist, unless the current page is the last.
  81. if ($current_page != count($elements)) {
  82. $last = array_pop($elements);
  83. $next = array_pop($elements);
  84. }
  85. // Verify items and links to pages.
  86. foreach ($elements as $page => $element) {
  87. // Make item/page index 1-based.
  88. $page++;
  89. if ($current_page == $page) {
  90. $this->assertClass($element, 'pager-current', 'Item for current page has .pager-current class.');
  91. $this->assertFalse(isset($element->a), 'Item for current page has no link.');
  92. }
  93. else {
  94. $this->assertNoClass($element, 'pager-current', "Item for page $page has no .pager-current class.");
  95. $this->assertClass($element, 'pager-item', "Item for page $page has .pager-item class.");
  96. $this->assertTrue($element->a, "Link to page $page found.");
  97. $this->assertNoClass($element->a, 'active', "Link to page $page is not active.");
  98. }
  99. unset($elements[--$page]);
  100. }
  101. // Verify that no other items remain untested.
  102. $this->assertTrue(empty($elements), 'All expected items found.');
  103. // Verify first/previous and next/last items and links.
  104. if (isset($first)) {
  105. $this->assertClass($first, 'pager-first', 'Item for first page has .pager-first class.');
  106. $this->assertTrue($first->a, 'Link to first page found.');
  107. $this->assertNoClass($first->a, 'active', 'Link to first page is not active.');
  108. }
  109. if (isset($previous)) {
  110. $this->assertClass($previous, 'pager-previous', 'Item for first page has .pager-previous class.');
  111. $this->assertTrue($previous->a, 'Link to previous page found.');
  112. $this->assertNoClass($previous->a, 'active', 'Link to previous page is not active.');
  113. }
  114. if (isset($next)) {
  115. $this->assertClass($next, 'pager-next', 'Item for next page has .pager-next class.');
  116. $this->assertTrue($next->a, 'Link to next page found.');
  117. $this->assertNoClass($next->a, 'active', 'Link to next page is not active.');
  118. }
  119. if (isset($last)) {
  120. $this->assertClass($last, 'pager-last', 'Item for last page has .pager-last class.');
  121. $this->assertTrue($last->a, 'Link to last page found.');
  122. $this->assertNoClass($last->a, 'active', 'Link to last page is not active.');
  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. * Asserts that an element does not have 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 assertNoClass(SimpleXMLElement $element, $class, $message = NULL) {
  152. if (!isset($message)) {
  153. $message = "Class .$class not found.";
  154. }
  155. $this->assertTrue(strpos($element['class'], $class) === FALSE, $message);
  156. }
  157. }