pager.test 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. * Asserts pager items and links.
  50. *
  51. * @param int $current_page
  52. * The current pager page the internal browser is on.
  53. */
  54. protected function assertPagerItems($current_page) {
  55. $elements = $this->xpath('//ul[@class=:class]/li', array(':class' => 'pager'));
  56. $this->assertTrue(!empty($elements), 'Pager found.');
  57. // Make current page 1-based.
  58. $current_page++;
  59. // Extract first/previous and next/last items.
  60. // first/previous only exist, if the current page is not the first.
  61. if ($current_page > 1) {
  62. $first = array_shift($elements);
  63. $previous = array_shift($elements);
  64. }
  65. // next/last always exist, unless the current page is the last.
  66. if ($current_page != count($elements)) {
  67. $last = array_pop($elements);
  68. $next = array_pop($elements);
  69. }
  70. // Verify items and links to pages.
  71. foreach ($elements as $page => $element) {
  72. // Make item/page index 1-based.
  73. $page++;
  74. if ($current_page == $page) {
  75. $this->assertClass($element, 'pager-current', 'Item for current page has .pager-current class.');
  76. $this->assertFalse(isset($element->a), 'Item for current page has no link.');
  77. }
  78. else {
  79. $this->assertNoClass($element, 'pager-current', "Item for page $page has no .pager-current class.");
  80. $this->assertClass($element, 'pager-item', "Item for page $page has .pager-item class.");
  81. $this->assertTrue($element->a, "Link to page $page found.");
  82. $this->assertNoClass($element->a, 'active', "Link to page $page is not active.");
  83. }
  84. unset($elements[--$page]);
  85. }
  86. // Verify that no other items remain untested.
  87. $this->assertTrue(empty($elements), 'All expected items found.');
  88. // Verify first/previous and next/last items and links.
  89. if (isset($first)) {
  90. $this->assertClass($first, 'pager-first', 'Item for first page has .pager-first class.');
  91. $this->assertTrue($first->a, 'Link to first page found.');
  92. $this->assertNoClass($first->a, 'active', 'Link to first page is not active.');
  93. }
  94. if (isset($previous)) {
  95. $this->assertClass($previous, 'pager-previous', 'Item for first page has .pager-previous class.');
  96. $this->assertTrue($previous->a, 'Link to previous page found.');
  97. $this->assertNoClass($previous->a, 'active', 'Link to previous page is not active.');
  98. }
  99. if (isset($next)) {
  100. $this->assertClass($next, 'pager-next', 'Item for next page has .pager-next class.');
  101. $this->assertTrue($next->a, 'Link to next page found.');
  102. $this->assertNoClass($next->a, 'active', 'Link to next page is not active.');
  103. }
  104. if (isset($last)) {
  105. $this->assertClass($last, 'pager-last', 'Item for last page has .pager-last class.');
  106. $this->assertTrue($last->a, 'Link to last page found.');
  107. $this->assertNoClass($last->a, 'active', 'Link to last page is not active.');
  108. }
  109. }
  110. /**
  111. * Asserts that an element has a given class.
  112. *
  113. * @param SimpleXMLElement $element
  114. * The element to test.
  115. * @param string $class
  116. * The class to assert.
  117. * @param string $message
  118. * (optional) A verbose message to output.
  119. */
  120. protected function assertClass(SimpleXMLElement $element, $class, $message = NULL) {
  121. if (!isset($message)) {
  122. $message = "Class .$class found.";
  123. }
  124. $this->assertTrue(strpos($element['class'], $class) !== FALSE, $message);
  125. }
  126. /**
  127. * Asserts that an element does not have 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 assertNoClass(SimpleXMLElement $element, $class, $message = NULL) {
  137. if (!isset($message)) {
  138. $message = "Class .$class not found.";
  139. }
  140. $this->assertTrue(strpos($element['class'], $class) === FALSE, $message);
  141. }
  142. }