You are here

class PanelsStorageTest in Panels 8.3

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

Tests the PageManagerPanelsStorage service.

@coversDefaultClass \Drupal\panels\Plugin\PanelsStorage\PageManagerPanelsStorage

@group PageManager

Hierarchy

Expanded class hierarchy of PanelsStorageTest

File

tests/src/Unit/PanelsStorageTest.php, line 28
Contains \Drupal\Tests\panels\Unit\PanelsStorageTest.

Namespace

Drupal\Tests\panels\Unit
View source
class PanelsStorageTest extends UnitTestCase {

  /**
   * @var \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $panelsDisplay;

  /**
   * @var \Drupal\page_manager\PageVariantInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $pageVariant;

  /**
   * @var \Drupal\page_manager\PageVariantInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $pageVariantNotPanels;

  /**
   * @var \Drupal\Core\Entity\EntityStorageInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $storage;

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->panelsDisplay = $this
      ->prophesize(PanelsDisplayVariant::class);
    $this->pageVariant = $this
      ->prophesize(PageVariantInterface::class);
    $this->pageVariant
      ->getVariantPlugin()
      ->willReturn($this->panelsDisplay
      ->reveal());
    $this->pageVariantNotPanels = $this
      ->prophesize(PageVariantInterface::class);
    $this->pageVariantNotPanels
      ->getContexts()
      ->shouldNotBeCalled();
    $non_panels_variant = $this
      ->prophesize(HttpStatusCodeDisplayVariant::class);
    $this->pageVariantNotPanels
      ->getVariantPlugin()
      ->willReturn($non_panels_variant
      ->reveal());
    $this->storage = $this
      ->prophesize(EntityStorageInterface::class);
    $this->entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $this->entityTypeManager
      ->getStorage('page_variant')
      ->willReturn($this->storage
      ->reveal());
  }

  /**
   * @covers ::load
   */
  public function testLoad() {

    // Make sure that the contexts are passed down (or not).
    $this->pageVariant
      ->getContexts()
      ->willReturn([]);
    $this->panelsDisplay
      ->setContexts([])
      ->shouldBeCalledTimes(1);
    $this->storage
      ->load('id_exists')
      ->willReturn($this->pageVariant
      ->reveal());
    $this->storage
      ->load('doesnt_exist')
      ->willReturn(NULL);
    $this->storage
      ->load('not_a_panel')
      ->willReturn($this->pageVariantNotPanels
      ->reveal());
    $panels_storage = new PageManagerPanelsStorage([], '', [], $this->entityTypeManager
      ->reveal());

    // Test the success condition.
    $this
      ->assertSame($this->panelsDisplay
      ->reveal(), $panels_storage
      ->load('id_exists'));

    // Should be NULL if it doesn't exist.
    $this
      ->assertNull($panels_storage
      ->load('doesnt_exist'));

    // Should also be NULL if it's not a PanelsDisplayVariant.
    $this
      ->assertNull($panels_storage
      ->load('not_a_panel'));
  }

  /**
   * @covers ::save
   */
  public function testSaveSuccessful() {
    $test_config = [
      'my_config' => '123',
    ];
    $this->panelsDisplay
      ->setConfiguration($test_config)
      ->shouldBeCalledTimes(1);
    $this->pageVariant
      ->save()
      ->shouldBeCalledTimes(1);
    $this->storage
      ->load('id_exists')
      ->willReturn($this->pageVariant
      ->reveal());
    $panels_display = $this
      ->prophesize(PanelsDisplayVariant::class);
    $panels_display
      ->getStorageId()
      ->willReturn('id_exists');
    $panels_display
      ->getConfiguration()
      ->willReturn($test_config);
    $panels_storage = new PageManagerPanelsStorage([], '', [], $this->entityTypeManager
      ->reveal());
    $panels_storage
      ->save($panels_display
      ->reveal());
  }

  /**
   * @covers ::save
   *
   * @expectedException \Exception
   * @expectedExceptionMessage Couldn't find page variant to store Panels display
   */
  public function testSaveDoesntExist() {
    $this->panelsDisplay
      ->setConfiguration()
      ->shouldNotBeCalled();
    $this->pageVariant
      ->save()
      ->shouldNotBeCalled();
    $this->storage
      ->load('doesnt_exist')
      ->willReturn(NULL);
    $panels_display = $this
      ->prophesize(PanelsDisplayVariant::class);
    $panels_display
      ->getStorageId()
      ->willReturn('doesnt_exist');
    $panels_display
      ->getConfiguration()
      ->shouldNotBeCalled();
    $panels_storage = new PageManagerPanelsStorage([], '', [], $this->entityTypeManager
      ->reveal());
    $panels_storage
      ->save($panels_display
      ->reveal());
  }

  /**
   * @covers ::save
   *
   * @expectedException \Exception
   * @expectedExceptionMessage Page variant doesn't use a Panels display variant
   */
  public function testSaveNotPanels() {
    $this->storage
      ->load('not_a_panel')
      ->willReturn($this->pageVariantNotPanels
      ->reveal());
    $this->panelsDisplay
      ->setConfiguration(Argument::cetera())
      ->shouldNotBeCalled();
    $this->pageVariant
      ->save()
      ->shouldNotBeCalled();
    $panels_display = $this
      ->prophesize(PanelsDisplayVariant::class);
    $panels_display
      ->getStorageId()
      ->willReturn('not_a_panel');
    $panels_display
      ->getConfiguration()
      ->shouldNotBeCalled();
    $panels_storage = new PageManagerPanelsStorage([], '', [], $this->entityTypeManager
      ->reveal());
    $panels_storage
      ->save($panels_display
      ->reveal());
  }

  /**
   * @covers ::access
   */
  public function testAccess() {
    $this->storage
      ->load('id_exists')
      ->willReturn($this->pageVariant
      ->reveal());
    $this->storage
      ->load('doesnt_exist')
      ->willReturn(NULL);
    $account = $this
      ->prophesize(AccountInterface::class);
    $this->pageVariant
      ->access('read', $account
      ->reveal(), TRUE)
      ->willReturn(AccessResult::allowed());
    $panels_storage = new PageManagerPanelsStorage([], '', [], $this->entityTypeManager
      ->reveal());

    // Test the access condition.
    $this
      ->assertEquals(AccessResult::allowed(), $panels_storage
      ->access('id_exists', 'read', $account
      ->reveal()));

    // Should be forbidden if it doesn't exist.
    $this
      ->assertEquals(AccessResult::forbidden(), $panels_storage
      ->access('doesnt_exist', 'read', $account
      ->reveal()));

    // Test that 'change layout' becomes 'update'.
    $this->pageVariant
      ->access('update', $account
      ->reveal(), TRUE)
      ->willReturn(AccessResult::allowed());
    $this
      ->assertEquals(AccessResult::allowed(), $panels_storage
      ->access('id_exists', 'change layout', $account
      ->reveal()));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PanelsStorageTest::$entityTypeManager protected property
PanelsStorageTest::$pageVariant protected property
PanelsStorageTest::$pageVariantNotPanels protected property
PanelsStorageTest::$panelsDisplay protected property
PanelsStorageTest::$storage protected property
PanelsStorageTest::setUp protected function Overrides UnitTestCase::setUp
PanelsStorageTest::testAccess public function @covers ::access
PanelsStorageTest::testLoad public function @covers ::load
PanelsStorageTest::testSaveDoesntExist public function @covers ::save
PanelsStorageTest::testSaveNotPanels public function @covers ::save
PanelsStorageTest::testSaveSuccessful public function @covers ::save
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.