diff --git a/src/PanelsStorage.php b/src/PanelsStorage.php index ae610b4..24cb76a 100644 --- a/src/PanelsStorage.php +++ b/src/PanelsStorage.php @@ -59,7 +59,7 @@ class PanelsStorage implements PanelsStorageInterface { $page_variant->save(); } else { - throw new \Exception("Couuldn't find page variant to store Panels display"); + throw new \Exception("Couldn't find page variant to store Panels display"); } } diff --git a/tests/src/Unit/PanelsStorageTest.php b/tests/src/Unit/PanelsStorageTest.php new file mode 100644 index 0000000..cec2e06 --- /dev/null +++ b/tests/src/Unit/PanelsStorageTest.php @@ -0,0 +1,189 @@ +panelsDisplay = $this->getMockBuilder('Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant') + ->disableOriginalConstructor() + ->getMock(); + + $this->httpStatusCode = $this->getMockBuilder('Drupal\page_manager\Plugin\DisplayVariant\HttpStatusCodeDisplayVariant') + ->disableOriginalConstructor() + ->getMock(); + + $this->pageVariant = $this->getMock('Drupal\page_manager\PageVariantInterface'); + $this->pageVariant->method('getVariantPlugin')->willReturn($this->panelsDisplay); + + $this->pageVariantNotPanels = $this->getMock('Drupal\page_manager\PageVariantInterface'); + $this->pageVariantNotPanels->method('getVariantPlugin')->willReturn($this->httpStatusCode); + $this->pageVariantNotPanels->expects($this->never())->method('getContexts'); + + $this->storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface'); + $this->storage->method('load')->willReturnMap([ + ['id_exists', $this->pageVariant], + ['doesnt_exist', NULL], + ['not_a_panel', $this->pageVariantNotPanels], + ]); + + $this->entityTypeManager = $this->getMock('Drupal\Core\Entity\EntityTypeManagerInterface'); + $this->entityTypeManager->method('getStorage')->with('page_variant')->willReturn($this->storage); + + } + + /** + * @covers ::load + */ + public function testLoad() { + // Make sure that the contexts are passed down (or not). + $this->pageVariant->expects($this->once())->method('getContexts')->willReturn([]); + $this->panelsDisplay->expects($this->once())->method('setContexts')->with([]); + $this->pageVariantNotPanels->expects($this->never())->method('getContexts'); + + $panels_storage = new PanelsStorage($this->entityTypeManager); + + // Test the success condition. + $this->assertEquals($this->panelsDisplay, $panels_storage->load('id_exists')); + + // Should be NULL if it doesn't exist. + $this->assertEquals(NULL, $panels_storage->load('doesnt_exist')); + + // Should also be NULL if it's not a PanelsDisplayVariant. + $this->assertEquals(NULL, $panels_storage->load('not_a_panel')); + } + + /** + * @covers ::save + */ + public function testSaveSuccessful() { + $test_config = ['my_config' => '123']; + $this->panelsDisplay->expects($this->once())->method('setConfiguration')->with($test_config); + $this->pageVariant->expects($this->once())->method('save'); + + /** @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $panels_display */ + $panels_display = $this->getMockBuilder('Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant') + ->disableOriginalConstructor() + ->getMock(); + $panels_display->expects($this->once())->method('getStorageId')->willReturn('id_exists'); + $panels_display->expects($this->once())->method('getConfiguration')->willReturn($test_config); + + $panels_storage = new PanelsStorage($this->entityTypeManager); + $panels_storage->save($panels_display); + } + + /** + * @covers ::save + * + * @expectedException \Exception + * @expectedExceptionMessage Couldn't find page variant to store Panels display + */ + public function testSaveDoesntExist() { + $this->panelsDisplay->expects($this->never())->method('setConfiguration'); + $this->pageVariant->expects($this->never())->method('save'); + + /** @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $panels_display */ + $panels_display = $this->getMockBuilder('Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant') + ->disableOriginalConstructor() + ->getMock(); + $panels_display->expects($this->once())->method('getStorageId')->willReturn('doesnt_exist'); + $panels_display->expects($this->never())->method('getConfiguration'); + + $panels_storage = new PanelsStorage($this->entityTypeManager); + $panels_storage->save($panels_display); + } + + /** + * @covers ::save + * + * @expectedException \Exception + * @expectedExceptionMessage Page variant doesn't use a Panels display variant + */ + public function testSaveNotPanels() { + $this->panelsDisplay->expects($this->never())->method('setConfiguration'); + $this->pageVariant->expects($this->never())->method('save'); + + /** @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $panels_display */ + $panels_display = $this->getMockBuilder('Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant') + ->disableOriginalConstructor() + ->getMock(); + $panels_display->expects($this->once())->method('getStorageId')->willReturn('not_a_panel'); + $panels_display->expects($this->never())->method('getConfiguration'); + + $panels_storage = new PanelsStorage($this->entityTypeManager); + $panels_storage->save($panels_display); + } + + /** + * @covers ::access + */ + public function testAccess() { + /** @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\Core\Session\AccountInterface $account */ + $account = $this->getMock('\Drupal\Core\Session\AccountInterface'); + + $this->pageVariant->expects($this->once()) + ->method('access') + ->with('read', $account, TRUE) + ->willReturn(AccessResult::allowed()); + + $panels_storage = new PanelsStorage($this->entityTypeManager); + + // Test the access condition. + $this->assertEquals(AccessResult::allowed(), $panels_storage->access('id_exists', 'read', $account)); + + // Should be forbidden if it doesn't exist. + $this->assertEquals(AccessResult::forbidden(), $panels_storage->access('doesnt_exist', 'read', $account)); + } + +}