You are here

public function PageTest::testRemoveVariant 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::testRemoveVariant()

@covers ::removeVariant

File

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

Class

PageTest
Tests the Page entity.

Namespace

Drupal\Tests\page_manager\Unit

Code

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);
}