View source
<?php
namespace Drupal\Tests\rabbit_hole\Functional;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\views\Functional\ViewTestBase;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\rabbit_hole\Plugin\RabbitHoleBehaviorPluginManager;
use Drupal\rabbit_hole\Plugin\RabbitHoleBehaviorPlugin\AccessDenied;
use Drupal\rabbit_hole\Plugin\RabbitHoleBehaviorPlugin\DisplayPage;
use Drupal\rabbit_hole\Plugin\RabbitHoleBehaviorPlugin\PageNotFound;
use Drupal\rabbit_hole\Plugin\RabbitHoleBehaviorPlugin\PageRedirect;
class RabbitHoleBehaviorPluginTest extends ViewTestBase {
const TEST_CONTENT_TYPE_ID = 'rh_test_content_type';
const TEST_NODE_NAME = 'rh_test_node';
protected $defaultTheme = 'stark';
public static $modules = [
'rabbit_hole',
'node',
];
private $manager;
private $entity;
protected function setUp($import_test_views = TRUE) {
parent::setUp(FALSE);
$this->manager = $this->container
->get('plugin.manager.rabbit_hole_behavior_plugin');
$this
->createTestContentType();
$this->entity = $this
->createTestEntity();
}
public function testPluginManager() {
$this
->assertNotNull($this->manager, 'Drupal plugin service returned a rabbit hole behavior service.');
$this
->assertInstanceOf(RabbitHoleBehaviorPluginManager::class, $this->manager);
$behaviors = $this->manager
->getDefinitions();
$this
->assertCount(4, $behaviors, 'There are 4 behaviors.');
$this
->assertTrue($this->manager
->hasDefinition('access_denied'), 'There is an access denied plugin');
$this
->assertArrayHasKey('label', $behaviors['access_denied'], 'The access denied plugin has a label');
$this
->assertTrue($this->manager
->hasDefinition('display_page'), 'There is a display the page plugin');
$this
->assertArrayHasKey('label', $behaviors['display_page'], 'The display the page plugin has a label');
$this
->assertTrue($this->manager
->hasDefinition('page_not_found'), 'There is a page not found plugin');
$this
->assertArrayHasKey('label', $behaviors['page_not_found'], 'The page not found plugin has a label');
$this
->assertTrue($this->manager
->hasDefinition('page_redirect'), 'There is a page redirect plugin');
$this
->assertArrayHasKey('label', $behaviors['page_redirect'], 'The page redirect plugin has a label');
}
public function testAccessDeniedPlugin() {
$plugin = $this->manager
->createInstance('access_denied', [
'of' => 'configuration values',
]);
$this
->assertInstanceOf(AccessDenied::class, $plugin, 'The access denied plugin is the correct type.');
$this
->expectException(AccessDeniedHttpException::class);
$plugin
->performAction($this->entity);
}
public function testDisplayPagePlugin() {
$plugin = $this->manager
->createInstance('display_page', [
'of' => 'configuration values',
]);
$this
->assertInstanceOf(DisplayPage::class, $plugin, 'The display page plugin is the correct type.');
$this
->assertEmpty($plugin
->performAction($this->entity));
}
public function testPageNotFoundPlugin() {
$plugin = $this->manager
->createInstance('page_not_found', [
'of' => 'configuration values',
]);
$this
->assertInstanceOf(PageNotFound::class, $plugin, 'The page not found plugin is the correct type.');
$this
->expectException(NotFoundHttpException::class);
$plugin
->performAction($this->entity);
}
public function testPageRedirectPlugin() {
$plugin = $this->manager
->createInstance('page_redirect', [
'of' => 'configuration values',
]);
$this
->assertInstanceOf(PageRedirect::class, $plugin, 'The page redirect plugin is the correct type.');
}
private function createTestContentType() {
$node_type = NodeType::create([
'type' => self::TEST_CONTENT_TYPE_ID,
'name' => self::TEST_CONTENT_TYPE_ID,
]);
$node_type
->save();
return $node_type;
}
private function createTestEntity() {
$node = Node::create([
'nid' => NULL,
'type' => self::TEST_CONTENT_TYPE_ID,
'title' => 'Test Behavior Settings Node',
]);
$node
->save();
return $node;
}
}