You are here

class PageTest in Page Manager 8

Same name and namespace in other branches
  1. 8.4 tests/src/Unit/PageTest.php \Drupal\Tests\page_manager\Unit\PageTest

Tests the Page entity.

@coversDefaultClass \Drupal\page_manager\Entity\Page

@group PageManager

Hierarchy

Expanded class hierarchy of PageTest

File

tests/src/Unit/PageTest.php, line 30
Contains \Drupal\Tests\page_manager\Unit\PageTest.

Namespace

Drupal\Tests\page_manager\Unit
View source
class PageTest extends UnitTestCase {

  /**
   * @var \Drupal\page_manager\Entity\Page
   */
  protected $page;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->page = new Page([
      'id' => 'the_page',
    ], 'page');
  }

  /**
   * @covers ::getVariants
   */
  public function testGetVariants() {
    $variant1 = $this
      ->prophesize(PageVariantInterface::class);
    $variant1
      ->id()
      ->willReturn('variant1');
    $variant1
      ->getWeight()
      ->willReturn(0);
    $variant2 = $this
      ->prophesize(PageVariantInterface::class);
    $variant2
      ->id()
      ->willReturn('variant2');
    $variant2
      ->getWeight()
      ->willReturn(-10);
    $entity_storage = $this
      ->prophesize(EntityStorageInterface::class);
    $entity_storage
      ->loadByProperties([
      'page' => 'the_page',
    ])
      ->willReturn([
      'variant1' => $variant1
        ->reveal(),
      'variant2' => $variant2
        ->reveal(),
    ])
      ->shouldBeCalledTimes(1);
    $entity_type_manager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $entity_type_manager
      ->getStorage('page_variant')
      ->willReturn($entity_storage);
    $container = new ContainerBuilder();
    $container
      ->set('entity_type.manager', $entity_type_manager
      ->reveal());
    \Drupal::setContainer($container);
    $variants = $this->page
      ->getVariants();
    $this
      ->assertSame([
      'variant2' => $variant2
        ->reveal(),
      'variant1' => $variant1
        ->reveal(),
    ], $variants);
    $variants = $this->page
      ->getVariants();
    $this
      ->assertSame([
      'variant2' => $variant2
        ->reveal(),
      'variant1' => $variant1
        ->reveal(),
    ], $variants);
  }

  /**
   * @covers ::addVariant
   */
  public function testAddVariant() {
    $variant1 = $this
      ->prophesize(PageVariantInterface::class);
    $variant1
      ->id()
      ->willReturn('variant1');
    $variant1
      ->getWeight()
      ->willReturn(0);
    $variant2 = $this
      ->prophesize(PageVariantInterface::class);
    $variant2
      ->id()
      ->willReturn('variant2');
    $variant2
      ->getWeight()
      ->willReturn(-10);
    $variant3 = $this
      ->prophesize(PageVariantInterface::class);
    $variant3
      ->id()
      ->willReturn('variant3');
    $variant3
      ->getWeight()
      ->willReturn(-5);
    $entity_storage = $this
      ->prophesize(EntityStorageInterface::class);
    $entity_storage
      ->loadByProperties([
      'page' => 'the_page',
    ])
      ->willReturn([
      'variant1' => $variant1
        ->reveal(),
      'variant2' => $variant2
        ->reveal(),
    ])
      ->shouldBeCalledTimes(1);
    $entity_type_manager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $entity_type_manager
      ->getStorage('page_variant')
      ->willReturn($entity_storage);
    $container = new ContainerBuilder();
    $container
      ->set('entity_type.manager', $entity_type_manager
      ->reveal());
    \Drupal::setContainer($container);

    // Check that Variant 1 and 2 are in the right order.
    $variants = $this->page
      ->getVariants();
    $this
      ->assertSame([
      'variant2' => $variant2
        ->reveal(),
      'variant1' => $variant1
        ->reveal(),
    ], $variants);

    // Add Variant 3.
    $this->page
      ->addVariant($variant3
      ->reveal());

    // Check that Variant 1, 2 and 3 are in the right order.
    $variants = $this->page
      ->getVariants();
    $this
      ->assertSame([
      'variant2' => $variant2
        ->reveal(),
      'variant3' => $variant3
        ->reveal(),
      'variant1' => $variant1
        ->reveal(),
    ], $variants);
  }

  /**
   * @covers ::removeVariant
   */
  public function testRemoveVariant() {
    $variant1 = $this
      ->prophesize(PageVariantInterface::class);
    $variant1
      ->id()
      ->willReturn('variant1');
    $variant1
      ->getWeight()
      ->willReturn(0);
    $variant1
      ->delete()
      ->shouldBeCalledTimes(1);
    $variant2 = $this
      ->prophesize(PageVariantInterface::class);
    $variant2
      ->id()
      ->willReturn('variant2');
    $variant2
      ->getWeight()
      ->willReturn(-10);
    $entity_storage = $this
      ->prophesize(EntityStorageInterface::class);
    $entity_storage
      ->loadByProperties([
      'page' => 'the_page',
    ])
      ->willReturn([
      'variant1' => $variant1
        ->reveal(),
      'variant2' => $variant2
        ->reveal(),
    ])
      ->shouldBeCalledTimes(1);
    $entity_type_manager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $entity_type_manager
      ->getStorage('page_variant')
      ->willReturn($entity_storage);
    $container = new ContainerBuilder();
    $container
      ->set('entity_type.manager', $entity_type_manager
      ->reveal());
    \Drupal::setContainer($container);

    // Check that Variant 1 and 2 are returned.
    $variants = $this->page
      ->getVariants();
    $this
      ->assertSame([
      'variant2' => $variant2
        ->reveal(),
      'variant1' => $variant1
        ->reveal(),
    ], $variants);

    // Remove Variant 1.
    $this->page
      ->removeVariant($variant1
      ->reveal()
      ->id());

    // Check that Variant 1 has been removed.
    $variants = $this->page
      ->getVariants();
    $this
      ->assertSame([
      'variant2' => $variant2
        ->reveal(),
    ], $variants);
  }

  /**
   * @covers ::addContext
   */
  public function testAddContext() {
    $context = new Context(new ContextDefinition('bar'));
    $this->page
      ->addContext('foo', $context);
    $contexts = $this->page
      ->getContexts();
    $this
      ->assertSame([
      'foo' => $context,
    ], $contexts);
  }

  /**
   * @covers ::getContexts
   */
  public function testGetContexts() {
    $context = new Context(new ContextDefinition('bar'));
    $event_dispatcher = $this
      ->prophesize(EventDispatcherInterface::class);
    $event_dispatcher
      ->dispatch(PageManagerEvents::PAGE_CONTEXT, Argument::type(PageManagerContextEvent::class))
      ->will(function ($args) use ($context) {
      $args[1]
        ->getPage()
        ->addContext('foo', $context);
    });
    $container = new ContainerBuilder();
    $container
      ->set('event_dispatcher', $event_dispatcher
      ->reveal());
    \Drupal::setContainer($container);
    $contexts = $this->page
      ->getContexts();
    $this
      ->assertSame([
      'foo' => $context,
    ], $contexts);
  }

  /**
   * @covers ::filterParameters
   */
  public function testFilterParameters() {

    // Changing filters clears cached contexts on variants so we have to setup
    // some variants for our page.
    $variant1 = $this
      ->prophesize(PageVariantInterface::class);
    $variant1
      ->id()
      ->willReturn('variant1');
    $variant1
      ->getWeight()
      ->willReturn(0);
    $variant1
      ->resetCollectedContexts()
      ->willReturn($variant1
      ->reveal());
    $variant2 = $this
      ->prophesize(PageVariantInterface::class);
    $variant2
      ->id()
      ->willReturn('variant2');
    $variant2
      ->getWeight()
      ->willReturn(-10);
    $variant2
      ->resetCollectedContexts()
      ->willReturn($variant2
      ->reveal());
    $entity_storage = $this
      ->prophesize(EntityStorageInterface::class);
    $entity_storage
      ->loadByProperties([
      'page' => 'the_page',
    ])
      ->willReturn([
      'variant1' => $variant1
        ->reveal(),
      'variant2' => $variant2
        ->reveal(),
    ])
      ->shouldBeCalledTimes(1);
    $entity_type_manager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $entity_type_manager
      ->getStorage('page_variant')
      ->willReturn($entity_storage);
    $container = new ContainerBuilder();
    $container
      ->set('entity_type.manager', $entity_type_manager
      ->reveal());
    \Drupal::setContainer($container);
    $parameters = [
      'foo' => [
        'machine_name' => 'foo',
        'type' => 'integer',
        'label' => 'Foo',
      ],
      'bar' => [
        'machine_name' => 'bar',
        'type' => '',
        'label' => '',
      ],
      'baz' => [
        'machine_name' => 'baz',
        'type' => 'integer',
        'label' => 'Baz',
      ],
    ];
    $page = new Page([
      'id' => 'the_page',
      'parameters' => $parameters,
      'path' => 'test/{foo}/{bar}',
    ], 'page');
    $expected = $parameters;
    unset($expected['baz']);
    $this
      ->assertEquals($expected, $page
      ->getParameters());
    $method = new \ReflectionMethod($page, 'filterParameters');
    $method
      ->setAccessible(TRUE);
    $method
      ->invoke($page);
    $expected = [
      'foo' => [
        'machine_name' => 'foo',
        'type' => 'integer',
        'label' => 'Foo',
      ],
    ];
    $this
      ->assertEquals($expected, $page
      ->get('parameters'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PageTest::$page protected property
PageTest::setUp protected function Overrides UnitTestCase::setUp
PageTest::testAddContext public function @covers ::addContext
PageTest::testAddVariant public function @covers ::addVariant
PageTest::testFilterParameters public function @covers ::filterParameters
PageTest::testGetContexts public function @covers ::getContexts
PageTest::testGetVariants public function @covers ::getVariants
PageTest::testRemoveVariant public function @covers ::removeVariant
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.