pager.test 5.9 KB

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