View source
<?php
namespace Drupal\Tests\rabbit_hole\Functional;
use Drupal\node\Entity\NodeType;
use Drupal\rabbit_hole\Entity\BehaviorSettings;
use Drupal\Tests\BrowserTestBase;
class RabbitHoleBehaviorSettingsTest extends BrowserTestBase {
const DEFAULT_TEST_ENTITY = 'node';
const DEFAULT_TEST_ENTITY_TYPE = 'node_type';
const DEFAULT_ACTION = 'bundle_default';
const DEFAULT_OVERRIDE = BehaviorSettings::OVERRIDE_ALLOW;
const DEFAULT_REDIRECT_CODE = BehaviorSettings::REDIRECT_NOT_APPLICABLE;
const DEFAULT_BUNDLE_ACTION = 'display_page';
const DEFAULT_BUNDLE_OVERRIDE = BehaviorSettings::OVERRIDE_ALLOW;
const DEFAULT_BUNDLE_REDIRECT_CODE = BehaviorSettings::REDIRECT_NOT_APPLICABLE;
protected $defaultTheme = 'stark';
public static $modules = [
'rabbit_hole',
self::DEFAULT_TEST_ENTITY,
];
private $behaviorSettingsManager;
private $configFactory;
private $testNodeType;
protected function setUp() {
parent::setUp();
$this->configFactory = $this->container
->get('config.factory');
$this->behaviorSettingsManager = $this->container
->get('rabbit_hole.behavior_settings_manager');
$this->testNodeType = $this
->generateTestNodeType();
}
public function testSettings() {
$this
->saveAndTestExpectedValues(self::DEFAULT_ACTION, __METHOD__, self::DEFAULT_TEST_ENTITY_TYPE, $this->testNodeType
->id());
}
public function testBundleSettingsDefault() {
$settings = \Drupal::config('rabbit_hole.behavior_settings.default');
$this
->assertEquals(self::DEFAULT_BUNDLE_ACTION, $settings
->get('action'), 'Unexpected default action');
$this
->assertEquals(self::DEFAULT_BUNDLE_OVERRIDE, $settings
->get('allow_override'), 'Unexpected default override');
$this
->assertEquals(self::DEFAULT_BUNDLE_REDIRECT_CODE, $settings
->get('redirect_code'), 'Unexpected default redirect');
}
public function testBundleSettings() {
$this
->saveAndTestExpectedValues('page_not_found', __METHOD__, self::DEFAULT_TEST_ENTITY_TYPE, $this->testNodeType
->id());
$this
->deleteTestNodeType();
}
public function testLoadBundleSettingsWithDefault() {
$action = $this->behaviorSettingsManager
->loadBehaviorSettingsAsConfig(self::DEFAULT_TEST_ENTITY, 'f4515736-cfa0-4e38-b3ed-1306f56bd2a1')
->get('action');
$this
->assertEquals($action, self::DEFAULT_BUNDLE_ACTION, 'Unexpected default action');
}
public function testLoadNullEditable() {
$editable = $this->behaviorSettingsManager
->loadBehaviorSettingsAsEditableConfig(self::DEFAULT_TEST_ENTITY, '6b92ed36-f17f-4799-97d0-ae1801ed37ff');
$this
->assertNull($editable);
}
private function saveAndTestExpectedValues($expected_action, $calling_method, $entity_type_label = '', $entity_id = NULL) {
$editable = $this->behaviorSettingsManager
->loadBehaviorSettingsAsEditableConfig($entity_type_label, $entity_id);
if (isset($editable)) {
$editable
->delete();
}
$this->behaviorSettingsManager
->saveBehaviorSettings([
'action' => $expected_action,
'allow_override' => 0,
'redirect_code' => 0,
'redirect' => '',
], $entity_type_label, $entity_id);
$action = $this->behaviorSettingsManager
->loadBehaviorSettingsAsConfig($entity_type_label, $entity_id)
->get('action');
$this
->assertEquals($expected_action, $action, 'Unexpected action ' . ' (called from ' . $calling_method . ')');
$this->behaviorSettingsManager
->loadBehaviorSettingsAsEditableConfig($entity_type_label, $entity_id)
->delete();
}
private function generateTestNodeType() {
$type = NodeType::create([
'type' => 'test_behavior_settings_node_type',
'name' => 'Test Behavior Settings Node Type',
]);
$type
->save();
return $type;
}
private function deleteTestNodeType() {
$this->testNodeType
->delete();
}
}