class BookManagerTest

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

@coversDefaultClass \Drupal\book\BookManager @group book

Hierarchy

Expanded class hierarchy of BookManagerTest

File

core/modules/book/tests/src/Unit/BookManagerTest.php, line 16

Namespace

Drupal\Tests\book\Unit
View source
class BookManagerTest extends UnitTestCase {
    
    /**
     * The mocked entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManager|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $entityTypeManager;
    
    /**
     * The mocked language manager.
     *
     * @var \Drupal\Core\Language\LanguageManager|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $languageManager;
    
    /**
     * The mocked entity repository.
     *
     * @var \Drupal\Core\Entity\EntityRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $entityRepository;
    
    /**
     * The mocked config factory.
     *
     * @var \Drupal\Core\Config\ConfigFactory|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $configFactory;
    
    /**
     * The mocked translation manager.
     *
     * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $translation;
    
    /**
     * The mocked renderer.
     *
     * @var \Drupal\Core\Render\RendererInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $renderer;
    
    /**
     * The tested book manager.
     *
     * @var \Drupal\book\BookManager
     */
    protected $bookManager;
    
    /**
     * Book outline storage.
     *
     * @var \Drupal\book\BookOutlineStorageInterface
     */
    protected $bookOutlineStorage;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
        $this->translation = $this->getStringTranslationStub();
        $this->configFactory = $this->getConfigFactoryStub([]);
        $this->bookOutlineStorage = $this->createMock('Drupal\\book\\BookOutlineStorageInterface');
        $this->renderer = $this->createMock('\\Drupal\\Core\\Render\\RendererInterface');
        $this->languageManager = $this->createMock('Drupal\\Core\\Language\\LanguageManagerInterface');
        $this->entityRepository = $this->createMock('Drupal\\Core\\Entity\\EntityRepositoryInterface');
        // Used for both book manager cache services: backend chain and memory.
        $cache = $this->createMock(CacheBackendInterface::class);
        $this->bookManager = new BookManager($this->entityTypeManager, $this->translation, $this->configFactory, $this->bookOutlineStorage, $this->renderer, $this->languageManager, $this->entityRepository, $cache, $cache);
    }
    
    /**
     * Tests the getBookParents() method.
     *
     * @dataProvider providerTestGetBookParents
     */
    public function testGetBookParents($book, $parent, $expected) {
        $this->assertEquals($expected, $this->bookManager
            ->getBookParents($book, $parent));
    }
    
    /**
     * Provides test data for testGetBookParents.
     *
     * @return array
     *   The test data.
     */
    public static function providerTestGetBookParents() {
        $empty = [
            'p1' => 0,
            'p2' => 0,
            'p3' => 0,
            'p4' => 0,
            'p5' => 0,
            'p6' => 0,
            'p7' => 0,
            'p8' => 0,
            'p9' => 0,
        ];
        return [
            // Provides a book without an existing parent.
[
                [
                    'pid' => 0,
                    'nid' => 12,
                ],
                [],
                [
                    'depth' => 1,
                    'p1' => 12,
                ] + $empty,
            ],
            // Provides a book with an existing parent.
[
                [
                    'pid' => 11,
                    'nid' => 12,
                ],
                [
                    'nid' => 11,
                    'depth' => 1,
                    'p1' => 11,
                ],
                [
                    'depth' => 2,
                    'p1' => 11,
                    'p2' => 12,
                ] + $empty,
            ],
            // Provides a book with two existing parents.
[
                [
                    'pid' => 11,
                    'nid' => 12,
                ],
                [
                    'nid' => 11,
                    'depth' => 2,
                    'p1' => 10,
                    'p2' => 11,
                ],
                [
                    'depth' => 3,
                    'p1' => 10,
                    'p2' => 11,
                    'p3' => 12,
                ] + $empty,
            ],
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
BookManagerTest::$bookManager protected property The tested book manager.
BookManagerTest::$bookOutlineStorage protected property Book outline storage.
BookManagerTest::$configFactory protected property The mocked config factory.
BookManagerTest::$entityRepository protected property The mocked entity repository.
BookManagerTest::$entityTypeManager protected property The mocked entity type manager.
BookManagerTest::$languageManager protected property The mocked language manager.
BookManagerTest::$renderer protected property The mocked renderer.
BookManagerTest::$translation protected property The mocked translation manager.
BookManagerTest::providerTestGetBookParents public static function Provides test data for testGetBookParents.
BookManagerTest::setUp protected function Overrides UnitTestCase::setUp
BookManagerTest::testGetBookParents public function Tests the getBookParents() method.
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.
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.