View source
<?php
namespace Drupal\Tests\panels\Unit;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\page_manager\PageVariantInterface;
use Drupal\panels\Plugin\PanelsStorage\PageManagerPanelsStorage;
use Drupal\page_manager\Plugin\DisplayVariant\HttpStatusCodeDisplayVariant;
use Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
class PanelsStorageTest extends UnitTestCase {
protected $panelsDisplay;
protected $pageVariant;
protected $pageVariantNotPanels;
protected $storage;
protected $entityTypeManager;
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());
}
public function testLoad() {
$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());
$this
->assertSame($this->panelsDisplay
->reveal(), $panels_storage
->load('id_exists'));
$this
->assertNull($panels_storage
->load('doesnt_exist'));
$this
->assertNull($panels_storage
->load('not_a_panel'));
}
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());
}
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());
}
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());
}
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());
$this
->assertEquals(AccessResult::allowed(), $panels_storage
->access('id_exists', 'read', $account
->reveal()));
$this
->assertEquals(AccessResult::forbidden(), $panels_storage
->access('doesnt_exist', 'read', $account
->reveal()));
$this->pageVariant
->access('update', $account
->reveal(), TRUE)
->willReturn(AccessResult::allowed());
$this
->assertEquals(AccessResult::allowed(), $panels_storage
->access('id_exists', 'change layout', $account
->reveal()));
}
}