- <?php
- * @file
- * Tests for pager functionality.
- */
-
- * Tests pager functionality.
- */
- class PagerFunctionalWebTestCase extends BackdropWebTestCase {
- protected $profile = 'testing';
-
-
- * @var User
- */
- protected $admin_user;
-
- function setUp() {
- parent::setUp(array('dblog'));
-
-
- for ($i = 0; $i < 300; $i++) {
- watchdog('pager_test', $this->randomString(), NULL, WATCHDOG_NOTICE);
- }
-
- $this->admin_user = $this->backdropCreateUser(array(
- 'access site reports',
- ));
- $this->backdropLogin($this->admin_user);
- }
-
-
- * Tests markup and CSS classes of pager links.
- */
- function testActiveClass() {
-
- $this->backdropGet('admin/reports/dblog');
- $current_page = 0;
- $this->assertPagerItems($current_page);
-
-
- $current_page++;
- $this->backdropGet('admin/reports/dblog', array('query' => array('page' => $current_page)));
- $this->assertPagerItems($current_page);
-
-
- $elements = $this->xpath('//li[contains(@class, :class)]/a', array(':class' => 'pager-last'));
- preg_match('@page=(\d+)@', $elements[0]['href'], $matches);
- $current_page = (int) $matches[1];
- $this->backdropGet($GLOBALS['base_root'] . $elements[0]['href'], array('external' => TRUE));
- $this->assertPagerItems($current_page);
- }
-
-
- * Tests theme_pager() when an empty quantity is passed.
- */
- public function testThemePagerQuantityNotSet() {
- $variables = array(
- 'element' => 0,
- 'parameters' => array(),
- 'quantity' => '',
- 'tags' => '',
- );
- pager_default_initialize(100, 10);
- $rendered_output = theme_pager($variables);
- $this->assertNotIdentical(stripos($rendered_output, 'next'), FALSE);
- $this->assertNotIdentical(stripos($rendered_output, 'last'), FALSE);
- }
-
-
- * Asserts pager items and links.
- *
- * @param int $current_page
- * The current pager page the internal browser is on.
- */
- protected function assertPagerItems($current_page) {
- $elements = $this->xpath('//ul[@class=:class]/li', array(':class' => 'pager'));
- $this->assertTrue(!empty($elements), 'Pager found.');
-
-
- $current_page++;
-
-
-
- if ($current_page > 1) {
- $first = array_shift($elements);
- $previous = array_shift($elements);
- }
-
- if ($current_page != count($elements)) {
- $last = array_pop($elements);
- $next = array_pop($elements);
- }
-
-
- foreach ($elements as $page => $element) {
-
- $page++;
- if ($current_page == $page) {
- $this->assertClass($element, 'pager-current', 'Item for current page has .pager-current class.');
- $this->assertFalse(isset($element->a), 'Item for current page has no link.');
- }
- else {
- $this->assertNoClass($element, 'pager-current', "Item for page $page has no .pager-current class.");
- $this->assertClass($element, 'pager-item', "Item for page $page has .pager-item class.");
- $this->assertTrue($element->a, "Link to page $page found.");
- $this->assertNoClass($element->a, 'active', "Link to page $page is not active.");
- }
- unset($elements[--$page]);
- }
-
- $this->assertTrue(empty($elements), 'All expected items found.');
-
-
- if (isset($first)) {
- $this->assertClass($first, 'pager-first', 'Item for first page has .pager-first class.');
- $this->assertTrue($first->a, 'Link to first page found.');
- $this->assertNoClass($first->a, 'active', 'Link to first page is not active.');
- }
- if (isset($previous)) {
- $this->assertClass($previous, 'pager-previous', 'Item for first page has .pager-previous class.');
- $this->assertTrue($previous->a, 'Link to previous page found.');
- $this->assertNoClass($previous->a, 'active', 'Link to previous page is not active.');
- }
- if (isset($next)) {
- $this->assertClass($next, 'pager-next', 'Item for next page has .pager-next class.');
- $this->assertTrue($next->a, 'Link to next page found.');
- $this->assertNoClass($next->a, 'active', 'Link to next page is not active.');
- }
- if (isset($last)) {
- $this->assertClass($last, 'pager-last', 'Item for last page has .pager-last class.');
- $this->assertTrue($last->a, 'Link to last page found.');
- $this->assertNoClass($last->a, 'active', 'Link to last page is not active.');
- }
- }
-
-
- * Asserts that an element has a given class.
- *
- * @param SimpleXMLElement $element
- * The element to test.
- * @param string $class
- * The class to assert.
- * @param string $message
- * (optional) A verbose message to output.
- */
- protected function assertClass(SimpleXMLElement $element, $class, $message = NULL) {
- if (!isset($message)) {
- $message = "Class .$class found.";
- }
- $this->assertTrue(strpos($element['class'], $class) !== FALSE, $message);
- }
-
-
- * Asserts that an element does not have a given class.
- *
- * @param SimpleXMLElement $element
- * The element to test.
- * @param string $class
- * The class to assert.
- * @param string $message
- * (optional) A verbose message to output.
- */
- protected function assertNoClass(SimpleXMLElement $element, $class, $message = NULL) {
- if (!isset($message)) {
- $message = "Class .$class not found.";
- }
- $this->assertTrue(strpos((string) $element['class'], $class) === FALSE, $message);
- }
- }