PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/system/src/Tests/Pager/PagerTest.php

https://gitlab.com/geeta7/drupal
PHP | 241 lines | 137 code | 27 blank | 77 comment | 17 complexity | 6f752a863c1626f6a217c26245b4b954 MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\system\Tests\Pager\PagerTest.
  5. */
  6. namespace Drupal\system\Tests\Pager;
  7. use Drupal\simpletest\WebTestBase;
  8. /**
  9. * Tests pager functionality.
  10. *
  11. * @group Pager
  12. */
  13. class PagerTest extends WebTestBase {
  14. /**
  15. * Modules to enable.
  16. *
  17. * @var array
  18. */
  19. public static $modules = array('dblog', 'pager_test');
  20. /**
  21. * A user with permission to access site reports.
  22. *
  23. * @var \Drupal\user\UserInterface
  24. */
  25. protected $adminUser;
  26. protected $profile = 'testing';
  27. protected function setUp() {
  28. parent::setUp();
  29. // Insert 300 log messages.
  30. $logger = $this->container->get('logger.factory')->get('pager_test');
  31. for ($i = 0; $i < 300; $i++) {
  32. $logger->debug($this->randomString());
  33. }
  34. $this->adminUser = $this->drupalCreateUser(array(
  35. 'access site reports',
  36. ));
  37. $this->drupalLogin($this->adminUser);
  38. }
  39. /**
  40. * Tests markup and CSS classes of pager links.
  41. */
  42. function testActiveClass() {
  43. // Verify first page.
  44. $this->drupalGet('admin/reports/dblog');
  45. $current_page = 0;
  46. $this->assertPagerItems($current_page);
  47. // Verify any page but first/last.
  48. $current_page++;
  49. $this->drupalGet('admin/reports/dblog', array('query' => array('page' => $current_page)));
  50. $this->assertPagerItems($current_page);
  51. // Verify last page.
  52. $elements = $this->xpath('//li[contains(@class, :class)]/a', array(':class' => 'pager__item--last'));
  53. preg_match('@page=(\d+)@', $elements[0]['href'], $matches);
  54. $current_page = (int) $matches[1];
  55. $this->drupalGet($GLOBALS['base_root'] . parse_url($this->getUrl())['path'] . $elements[0]['href'], array('external' => TRUE));
  56. $this->assertPagerItems($current_page);
  57. }
  58. /**
  59. * Test proper functioning of the query parameters and the pager cache context.
  60. */
  61. protected function testPagerQueryParametersAndCacheContext() {
  62. // First page.
  63. $this->drupalGet('pager-test/query-parameters');
  64. $this->assertText(t('Pager calls: 0'), 'Initial call to pager shows 0 calls.');
  65. $this->assertText('[url.query_args.pagers:0]=0.0');
  66. $this->assertCacheContext('url.query_args');
  67. // Go to last page, the count of pager calls need to go to 1.
  68. $elements = $this->xpath('//li[contains(@class, :class)]/a', array(':class' => 'pager__item--last'));
  69. $this->drupalGet($this->getAbsoluteUrl($elements[0]['href']));
  70. $this->assertText(t('Pager calls: 1'), 'First link call to pager shows 1 calls.');
  71. $this->assertText('[url.query_args.pagers:0]=0.60');
  72. $this->assertCacheContext('url.query_args');
  73. // Go back to first page, the count of pager calls need to go to 2.
  74. $elements = $this->xpath('//li[contains(@class, :class)]/a', array(':class' => 'pager__item--first'));
  75. $this->drupalGet($this->getAbsoluteUrl($elements[0]['href']));
  76. $this->drupalGet($GLOBALS['base_root'] . parse_url($this->getUrl())['path'] . $elements[0]['href'], array('external' => TRUE));
  77. $this->assertText(t('Pager calls: 2'), 'Second link call to pager shows 2 calls.');
  78. $this->assertText('[url.query_args.pagers:0]=0.0');
  79. $this->assertCacheContext('url.query_args');
  80. }
  81. /**
  82. * Test proper functioning of the ellipsis.
  83. */
  84. public function testPagerEllipsis() {
  85. // Insert 100 extra log messages to get 9 pages.
  86. $logger = $this->container->get('logger.factory')->get('pager_test');
  87. for ($i = 0; $i < 100; $i++) {
  88. $logger->debug($this->randomString());
  89. }
  90. $this->drupalGet('admin/reports/dblog');
  91. $elements = $this->cssSelect(".pager__item--ellipsis:contains('…')");
  92. $this->assertEqual(count($elements), 0, 'No ellipsis has been set.');
  93. // Insert an extra 50 log messages to get 10 pages.
  94. $logger = $this->container->get('logger.factory')->get('pager_test');
  95. for ($i = 0; $i < 50; $i++) {
  96. $logger->debug($this->randomString());
  97. }
  98. $this->drupalGet('admin/reports/dblog');
  99. $elements = $this->cssSelect(".pager__item--ellipsis:contains('…')");
  100. $this->assertEqual(count($elements), 1, 'Found the ellipsis.');
  101. }
  102. /**
  103. * Asserts pager items and links.
  104. *
  105. * @param int $current_page
  106. * The current pager page the internal browser is on.
  107. */
  108. protected function assertPagerItems($current_page) {
  109. $elements = $this->xpath('//ul[contains(@class, :class)]/li', array(':class' => 'pager__items'));
  110. $this->assertTrue(!empty($elements), 'Pager found.');
  111. // Make current page 1-based.
  112. $current_page++;
  113. // Extract first/previous and next/last items.
  114. // first/previous only exist, if the current page is not the first.
  115. if ($current_page > 1) {
  116. $first = array_shift($elements);
  117. $previous = array_shift($elements);
  118. }
  119. // next/last always exist, unless the current page is the last.
  120. if ($current_page != count($elements)) {
  121. $last = array_pop($elements);
  122. $next = array_pop($elements);
  123. }
  124. // We remove elements from the $elements array in the following code, so
  125. // we store the total number of pages for verifying the "last" link.
  126. $total_pages = count($elements);
  127. // Verify items and links to pages.
  128. foreach ($elements as $page => $element) {
  129. // Make item/page index 1-based.
  130. $page++;
  131. if ($current_page == $page) {
  132. $this->assertClass($element, 'is-active', 'Element for current page has .is-active class.');
  133. $this->assertTrue($element->a, 'Element for current page has link.');
  134. $destination = $element->a['href'][0]->__toString();
  135. // URL query string param is 0-indexed.
  136. $this->assertEqual($destination, '?page=' . ($page - 1));
  137. }
  138. else {
  139. $this->assertNoClass($element, 'is-active', "Element for page $page has no .is-active class.");
  140. $this->assertClass($element, 'pager__item', "Element for page $page has .pager__item class.");
  141. $this->assertTrue($element->a, "Link to page $page found.");
  142. $destination = $element->a['href'][0]->__toString();
  143. $this->assertEqual($destination, '?page=' . ($page - 1));
  144. }
  145. unset($elements[--$page]);
  146. }
  147. // Verify that no other items remain untested.
  148. $this->assertTrue(empty($elements), 'All expected items found.');
  149. // Verify first/previous and next/last items and links.
  150. if (isset($first)) {
  151. $this->assertClass($first, 'pager__item--first', 'Element for first page has .pager__item--first class.');
  152. $this->assertTrue($first->a, 'Link to first page found.');
  153. $this->assertNoClass($first->a, 'is-active', 'Link to first page is not active.');
  154. $destination = $first->a['href'][0]->__toString();
  155. $this->assertEqual($destination, '?page=0');
  156. }
  157. if (isset($previous)) {
  158. $this->assertClass($previous, 'pager__item--previous', 'Element for first page has .pager__item--previous class.');
  159. $this->assertTrue($previous->a, 'Link to previous page found.');
  160. $this->assertNoClass($previous->a, 'is-active', 'Link to previous page is not active.');
  161. $destination = $previous->a['href'][0]->__toString();
  162. // URL query string param is 0-indexed, $current_page is 1-indexed.
  163. $this->assertEqual($destination, '?page=' . ($current_page - 2));
  164. }
  165. if (isset($next)) {
  166. $this->assertClass($next, 'pager__item--next', 'Element for next page has .pager__item--next class.');
  167. $this->assertTrue($next->a, 'Link to next page found.');
  168. $this->assertNoClass($next->a, 'is-active', 'Link to next page is not active.');
  169. $destination = $next->a['href'][0]->__toString();
  170. // URL query string param is 0-indexed, $current_page is 1-indexed.
  171. $this->assertEqual($destination, '?page=' . $current_page);
  172. }
  173. if (isset($last)) {
  174. $this->assertClass($last, 'pager__item--last', 'Element for last page has .pager__item--last class.');
  175. $this->assertTrue($last->a, 'Link to last page found.');
  176. $this->assertNoClass($last->a, 'is-active', 'Link to last page is not active.');
  177. $destination = $last->a['href'][0]->__toString();
  178. // URL query string param is 0-indexed.
  179. $this->assertEqual($destination, '?page=' . ($total_pages - 1));
  180. }
  181. }
  182. /**
  183. * Asserts that an element has a given class.
  184. *
  185. * @param \SimpleXMLElement $element
  186. * The element to test.
  187. * @param string $class
  188. * The class to assert.
  189. * @param string $message
  190. * (optional) A verbose message to output.
  191. */
  192. protected function assertClass(\SimpleXMLElement $element, $class, $message = NULL) {
  193. if (!isset($message)) {
  194. $message = "Class .$class found.";
  195. }
  196. $this->assertTrue(strpos($element['class'], $class) !== FALSE, $message);
  197. }
  198. /**
  199. * Asserts that an element does not have a given class.
  200. *
  201. * @param \SimpleXMLElement $element
  202. * The element to test.
  203. * @param string $class
  204. * The class to assert.
  205. * @param string $message
  206. * (optional) A verbose message to output.
  207. */
  208. protected function assertNoClass(\SimpleXMLElement $element, $class, $message = NULL) {
  209. if (!isset($message)) {
  210. $message = "Class .$class not found.";
  211. }
  212. $this->assertTrue(strpos($element['class'], $class) === FALSE, $message);
  213. }
  214. }