class PreprocessPagerTest

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Unit/Pager/PreprocessPagerTest.php \Drupal\Tests\system\Unit\Pager\PreprocessPagerTest
  2. 8.9.x core/modules/system/tests/src/Unit/Pager/PreprocessPagerTest.php \Drupal\Tests\system\Unit\Pager\PreprocessPagerTest
  3. 10 core/modules/system/tests/src/Unit/Pager/PreprocessPagerTest.php \Drupal\Tests\system\Unit\Pager\PreprocessPagerTest

Tests pager preprocessing.

@group system

@coversDefaultClass \Drupal\Core\Pager\PagerPreprocess

Hierarchy

Expanded class hierarchy of PreprocessPagerTest

File

core/modules/system/tests/src/Unit/Pager/PreprocessPagerTest.php, line 19

Namespace

Drupal\Tests\system\Unit\Pager
View source
class PreprocessPagerTest extends UnitTestCase {
  
  /**
   * Pager preprocess instance.
   */
  protected PagerPreprocess $pagerPreprocess;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $pager_manager = $this->getMockBuilder('Drupal\\Core\\Pager\\PagerManager')
      ->disableOriginalConstructor()
      ->getMock();
    $pager = $this->getMockBuilder('Drupal\\Core\\Pager\\Pager')
      ->disableOriginalConstructor()
      ->getMock();
    $url_generator = $this->getMockBuilder('Drupal\\Core\\Routing\\UrlGenerator')
      ->disableOriginalConstructor()
      ->getMock();
    $pager->method('getTotalPages')
      ->willReturn(2);
    $pager->method('getCurrentPage')
      ->willReturn(1);
    $url_generator->method('generateFromRoute')
      ->willReturn('');
    $pager_manager->method('getPager')
      ->willReturn($pager);
    $pager_manager->method('getUpdatedParameters')
      ->willReturn('');
    $this->pagerPreprocess = new PagerPreprocess($pager_manager);
    $container = new ContainerBuilder();
    $container->set('url_generator', $url_generator);
    \Drupal::setContainer($container);
  }
  
  /**
   * Tests when an empty #quantity is passed.
   *
   * @covers ::preprocessPager
   */
  public function testQuantityNotSet() : void {
    $variables = [
      'pager' => [
        '#element' => '',
        '#parameters' => [],
        '#quantity' => '',
        '#route_name' => '',
        '#tags' => '',
      ],
    ];
    $this->pagerPreprocess
      ->preprocessPager($variables);
    $this->assertEquals([
      'first',
      'previous',
    ], array_keys($variables['items']));
  }
  
  /**
   * Tests when a #quantity value is passed.
   *
   * @covers ::preprocessPager
   */
  public function testQuantitySet() : void {
    $variables = [
      'pager' => [
        '#element' => '2',
        '#parameters' => [],
        '#quantity' => '2',
        '#route_name' => '',
        '#tags' => '',
      ],
    ];
    $this->pagerPreprocess
      ->preprocessPager($variables);
    $this->assertEquals([
      'first',
      'previous',
      'pages',
    ], array_keys($variables['items']));
    /** @var \Drupal\Core\Template\AttributeString $attribute */
    $attribute = $variables['items']['pages']['2']['attributes']->offsetGet('aria-current');
    $this->assertInstanceOf(AttributeString::class, $attribute);
    $this->assertEquals('page', $attribute->value());
  }
  
  /**
   * Tests when an empty #pagination_heading_level value is passed.
   *
   * @covers ::preprocessPager
   */
  public function testEmptyPaginationHeadingLevelSet() : void {
    $variables = [
      'pager' => [
        '#element' => '2',
        '#pagination_heading_level' => '',
        '#parameters' => [],
        '#quantity' => '2',
        '#route_name' => '',
        '#tags' => '',
      ],
    ];
    $this->pagerPreprocess
      ->preprocessPager($variables);
    $this->assertEquals('h4', $variables['pagination_heading_level']);
  }
  
  /**
   * Tests when no #pagination_heading_level is passed.
   *
   * @covers ::preprocessPager
   */
  public function testPaginationHeadingLevelNotSet() : void {
    $variables = [
      'pager' => [
        '#element' => '',
        '#parameters' => [],
        '#quantity' => '',
        '#route_name' => '',
        '#tags' => '',
      ],
    ];
    $this->pagerPreprocess
      ->preprocessPager($variables);
    $this->assertEquals('h4', $variables['pagination_heading_level']);
  }
  
  /**
   * Tests when a #pagination_heading_level value is passed.
   *
   * @covers ::preprocessPager
   */
  public function testPaginationHeadingLevelSet() : void {
    $variables = [
      'pager' => [
        '#element' => '2',
        '#pagination_heading_level' => 'h5',
        '#parameters' => [],
        '#quantity' => '2',
        '#route_name' => '',
        '#tags' => '',
      ],
    ];
    $this->pagerPreprocess
      ->preprocessPager($variables);
    $this->assertEquals('h5', $variables['pagination_heading_level']);
  }
  
  /**
   * Test with an invalid #pagination_heading_level.
   *
   * @covers ::preprocessPager
   */
  public function testPaginationHeadingLevelInvalid() : void {
    $variables = [
      'pager' => [
        '#element' => '2',
        '#pagination_heading_level' => 'not-a-heading-element',
        '#parameters' => [],
        '#quantity' => '2',
        '#route_name' => '',
        '#tags' => '',
      ],
    ];
    $this->pagerPreprocess
      ->preprocessPager($variables);
    $this->assertEquals('h4', $variables['pagination_heading_level']);
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
PreprocessPagerTest::$pagerPreprocess protected property Pager preprocess instance.
PreprocessPagerTest::setUp protected function Overrides UnitTestCase::setUp
PreprocessPagerTest::testEmptyPaginationHeadingLevelSet public function Tests when an empty #pagination_heading_level value is passed.
PreprocessPagerTest::testPaginationHeadingLevelInvalid public function Test with an invalid #pagination_heading_level.
PreprocessPagerTest::testPaginationHeadingLevelNotSet public function Tests when no #pagination_heading_level is passed.
PreprocessPagerTest::testPaginationHeadingLevelSet public function Tests when a #pagination_heading_level value is passed.
PreprocessPagerTest::testQuantityNotSet public function Tests when an empty #quantity is passed.
PreprocessPagerTest::testQuantitySet public function Tests when a #quantity value is passed.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.