class SearchPageRepositoryTest

Same name in other branches
  1. 9 core/modules/search/tests/src/Unit/SearchPageRepositoryTest.php \Drupal\Tests\search\Unit\SearchPageRepositoryTest
  2. 8.9.x core/modules/search/tests/src/Unit/SearchPageRepositoryTest.php \Drupal\Tests\search\Unit\SearchPageRepositoryTest
  3. 10 core/modules/search/tests/src/Unit/SearchPageRepositoryTest.php \Drupal\Tests\search\Unit\SearchPageRepositoryTest

@coversDefaultClass \Drupal\search\SearchPageRepository @group search

Hierarchy

Expanded class hierarchy of SearchPageRepositoryTest

File

core/modules/search/tests/src/Unit/SearchPageRepositoryTest.php, line 17

Namespace

Drupal\Tests\search\Unit
View source
class SearchPageRepositoryTest extends UnitTestCase {
    
    /**
     * The search page repository.
     *
     * @var \Drupal\search\SearchPageRepository
     */
    protected $searchPageRepository;
    
    /**
     * The entity query object.
     *
     * @var \Drupal\Core\Entity\Query\QueryInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $query;
    
    /**
     * The search page storage.
     *
     * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $storage;
    
    /**
     * The config factory.
     *
     * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $configFactory;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->query = $this->createMock('Drupal\\Core\\Entity\\Query\\QueryInterface');
        $this->storage = $this->createMock('Drupal\\Core\\Config\\Entity\\ConfigEntityStorageInterface');
        $this->storage
            ->expects($this->any())
            ->method('getQuery')
            ->willReturn($this->query);
        
        /** @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject $entity_type_manager */
        $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
        $entity_type_manager->expects($this->any())
            ->method('getStorage')
            ->willReturn($this->storage);
        $this->configFactory = $this->createMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
        $this->searchPageRepository = new SearchPageRepository($this->configFactory, $entity_type_manager);
    }
    
    /**
     * Tests the getActiveSearchPages() method.
     */
    public function testGetActiveSearchPages() : void {
        $this->query
            ->expects($this->once())
            ->method('condition')
            ->with('status', TRUE)
            ->willReturn($this->query);
        $this->query
            ->expects($this->once())
            ->method('execute')
            ->willReturn([
            'test' => 'test',
            'other_test' => 'other_test',
        ]);
        $entities = [];
        $entities['test'] = $this->createMock('Drupal\\search\\SearchPageInterface');
        $entities['other_test'] = $this->createMock('Drupal\\search\\SearchPageInterface');
        $this->storage
            ->expects($this->once())
            ->method('loadMultiple')
            ->with([
            'test' => 'test',
            'other_test' => 'other_test',
        ])
            ->willReturn($entities);
        $result = $this->searchPageRepository
            ->getActiveSearchPages();
        $this->assertSame($entities, $result);
    }
    
    /**
     * Tests the isSearchActive() method.
     */
    public function testIsSearchActive() : void {
        $this->query
            ->expects($this->once())
            ->method('condition')
            ->with('status', TRUE)
            ->willReturn($this->query);
        $this->query
            ->expects($this->once())
            ->method('range')
            ->with(0, 1)
            ->willReturn($this->query);
        $this->query
            ->expects($this->once())
            ->method('execute')
            ->willReturn([
            'test' => 'test',
        ]);
        $this->assertTrue($this->searchPageRepository
            ->isSearchActive());
    }
    
    /**
     * Tests the getIndexableSearchPages() method.
     */
    public function testGetIndexableSearchPages() : void {
        $this->query
            ->expects($this->once())
            ->method('condition')
            ->with('status', TRUE)
            ->willReturn($this->query);
        $this->query
            ->expects($this->once())
            ->method('execute')
            ->willReturn([
            'test' => 'test',
            'other_test' => 'other_test',
        ]);
        $entities = [];
        $entities['test'] = $this->createMock('Drupal\\search\\SearchPageInterface');
        $entities['test']->expects($this->once())
            ->method('isIndexable')
            ->willReturn(TRUE);
        $entities['other_test'] = $this->createMock('Drupal\\search\\SearchPageInterface');
        $entities['other_test']->expects($this->once())
            ->method('isIndexable')
            ->willReturn(FALSE);
        $this->storage
            ->expects($this->once())
            ->method('loadMultiple')
            ->with([
            'test' => 'test',
            'other_test' => 'other_test',
        ])
            ->willReturn($entities);
        $result = $this->searchPageRepository
            ->getIndexableSearchPages();
        $this->assertCount(1, $result);
        $this->assertSame($entities['test'], reset($result));
    }
    
    /**
     * Tests the clearDefaultSearchPage() method.
     */
    public function testClearDefaultSearchPage() : void {
        $config = $this->getMockBuilder('Drupal\\Core\\Config\\Config')
            ->disableOriginalConstructor()
            ->getMock();
        $config->expects($this->once())
            ->method('clear')
            ->with('default_page')
            ->willReturn($config);
        $this->configFactory
            ->expects($this->once())
            ->method('getEditable')
            ->with('search.settings')
            ->willReturn($config);
        $this->searchPageRepository
            ->clearDefaultSearchPage();
    }
    
    /**
     * Tests the getDefaultSearchPage() method when the default is active.
     */
    public function testGetDefaultSearchPageWithActiveDefault() : void {
        $this->query
            ->expects($this->once())
            ->method('condition')
            ->with('status', TRUE)
            ->willReturn($this->query);
        $this->query
            ->expects($this->once())
            ->method('execute')
            ->willReturn([
            'test' => 'test',
            'other_test' => 'other_test',
        ]);
        $config = $this->getMockBuilder('Drupal\\Core\\Config\\Config')
            ->disableOriginalConstructor()
            ->getMock();
        $config->expects($this->once())
            ->method('get')
            ->with('default_page')
            ->willReturn('test');
        $this->configFactory
            ->expects($this->once())
            ->method('get')
            ->with('search.settings')
            ->willReturn($config);
        $this->assertSame('test', $this->searchPageRepository
            ->getDefaultSearchPage());
    }
    
    /**
     * Tests the getDefaultSearchPage() method when the default is inactive.
     */
    public function testGetDefaultSearchPageWithInactiveDefault() : void {
        $this->query
            ->expects($this->once())
            ->method('condition')
            ->with('status', TRUE)
            ->willReturn($this->query);
        $this->query
            ->expects($this->once())
            ->method('execute')
            ->willReturn([
            'test' => 'test',
        ]);
        $config = $this->getMockBuilder('Drupal\\Core\\Config\\Config')
            ->disableOriginalConstructor()
            ->getMock();
        $config->expects($this->once())
            ->method('get')
            ->with('default_page')
            ->willReturn('other_test');
        $this->configFactory
            ->expects($this->once())
            ->method('get')
            ->with('search.settings')
            ->willReturn($config);
        $this->assertSame('test', $this->searchPageRepository
            ->getDefaultSearchPage());
    }
    
    /**
     * Tests the setDefaultSearchPage() method.
     */
    public function testSetDefaultSearchPage() : void {
        $id = 'bananas';
        $config = $this->getMockBuilder('Drupal\\Core\\Config\\Config')
            ->disableOriginalConstructor()
            ->getMock();
        $config->expects($this->once())
            ->method('set')
            ->with('default_page', $id)
            ->willReturn($config);
        $config->expects($this->once())
            ->method('save')
            ->willReturn($config);
        $this->configFactory
            ->expects($this->once())
            ->method('getEditable')
            ->with('search.settings')
            ->willReturn($config);
        $search_page = $this->createMock('Drupal\\search\\SearchPageInterface');
        $search_page->expects($this->once())
            ->method('id')
            ->willReturn($id);
        $search_page->expects($this->once())
            ->method('enable')
            ->willReturn($search_page);
        $search_page->expects($this->once())
            ->method('save')
            ->willReturn($search_page);
        $this->searchPageRepository
            ->setDefaultSearchPage($search_page);
    }
    
    /**
     * Tests the sortSearchPages() method.
     */
    public function testSortSearchPages() : void {
        $entity_type = $this->createMock(EntityTypeInterface::class);
        $entity_type->method('getClass')
            ->willReturn(TestSearchPage::class);
        $this->storage
            ->expects($this->once())
            ->method('getEntityType')
            ->willReturn($entity_type);
        // Declare entities out of their expected order, so we can be sure they were
        // sorted.
        $entity_test4 = $this->createMock(TestSearchPage::class);
        $entity_test4->method('label')
            ->willReturn('Test4');
        $entity_test4->method('status')
            ->willReturn(FALSE);
        $entity_test4->method('getWeight')
            ->willReturn(0);
        $entity_test3 = $this->createMock(TestSearchPage::class);
        $entity_test3->method('label')
            ->willReturn('Test3');
        $entity_test3->method('status')
            ->willReturn(FALSE);
        $entity_test3->method('getWeight')
            ->willReturn(10);
        $entity_test2 = $this->createMock(TestSearchPage::class);
        $entity_test2->method('label')
            ->willReturn('Test2');
        $entity_test2->method('status')
            ->willReturn(TRUE);
        $entity_test2->method('getWeight')
            ->willReturn(0);
        $entity_test1 = $this->createMock(TestSearchPage::class);
        $entity_test1->method('label')
            ->willReturn('Test1');
        $entity_test1->method('status')
            ->willReturn(TRUE);
        $entity_test1->method('getWeight')
            ->willReturn(0);
        $unsorted_entities = [
            $entity_test4,
            $entity_test3,
            $entity_test2,
            $entity_test1,
        ];
        $expected = [
            $entity_test1,
            $entity_test2,
            $entity_test3,
            $entity_test4,
        ];
        $sorted_entities = $this->searchPageRepository
            ->sortSearchPages($unsorted_entities);
        $this->assertSame($expected, array_values($sorted_entities));
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
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.
SearchPageRepositoryTest::$configFactory protected property The config factory.
SearchPageRepositoryTest::$query protected property The entity query object.
SearchPageRepositoryTest::$searchPageRepository protected property The search page repository.
SearchPageRepositoryTest::$storage protected property The search page storage.
SearchPageRepositoryTest::setUp protected function Overrides UnitTestCase::setUp
SearchPageRepositoryTest::testClearDefaultSearchPage public function Tests the clearDefaultSearchPage() method.
SearchPageRepositoryTest::testGetActiveSearchPages public function Tests the getActiveSearchPages() method.
SearchPageRepositoryTest::testGetDefaultSearchPageWithActiveDefault public function Tests the getDefaultSearchPage() method when the default is active.
SearchPageRepositoryTest::testGetDefaultSearchPageWithInactiveDefault public function Tests the getDefaultSearchPage() method when the default is inactive.
SearchPageRepositoryTest::testGetIndexableSearchPages public function Tests the getIndexableSearchPages() method.
SearchPageRepositoryTest::testIsSearchActive public function Tests the isSearchActive() method.
SearchPageRepositoryTest::testSetDefaultSearchPage public function Tests the setDefaultSearchPage() method.
SearchPageRepositoryTest::testSortSearchPages public function Tests the sortSearchPages() method.
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::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
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::setUpBeforeClass public static function

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