View source
<?php
namespace Drupal\Tests\rabbit_hole\Functional;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\rabbit_hole\Entity\BehaviorSettings;
use Drupal\Tests\BrowserTestBase;
abstract class RabbitHoleBehaviorSettingsFormTestBase extends BrowserTestBase {
const DEFAULT_BUNDLE_ACTION = 'display_page';
const DEFAULT_ACTION = 'bundle_default';
protected $defaultTheme = 'stark';
public static $modules = [
'rabbit_hole',
];
protected $adminUser;
protected $bundleEntityTypeName;
protected $behaviorSettingsManager;
protected function setUp() {
parent::setUp();
$this->behaviorSettingsManager = $this->container
->get('rabbit_hole.behavior_settings_manager');
$admin_permissions = array_merge($this
->getAdminPermissions(), [
'rabbit hole administer ' . $this->entityType,
]);
$this->adminUser = $this
->drupalCreateUser($admin_permissions);
}
public function testDefaultBundleForm() {
$bundle_id = $this
->createEntityBundle();
$this
->loadEntityBundleForm($bundle_id);
$this
->assertRabbitHoleSettings();
$this
->assertSession()
->fieldValueEquals('rh_override', BehaviorSettings::OVERRIDE_ALLOW);
$this
->assertSession()
->checkboxChecked($this
->getOptionId(static::DEFAULT_BUNDLE_ACTION));
}
public function testAdministerPermission() {
$this
->drupalLogin($this
->drupalCreateUser($this
->getAdminPermissions()));
$this
->createEntityBundle();
$this
->drupalGet($this
->getCreateEntityUrl());
$this
->assertNoRabbitHoleSettings();
}
public function testBundleCreation() {
$override = BehaviorSettings::OVERRIDE_DISALLOW;
$action = 'access_denied';
$bundle_id = $this
->createEntityBundleFormSubmit($action, $override);
$saved_config = $this->behaviorSettingsManager
->loadBehaviorSettingsAsConfig($this->bundleEntityTypeName, $bundle_id);
$this
->assertEquals($action, $saved_config
->get('action'));
$this
->assertEquals($override, $saved_config
->get('allow_override'));
$this
->loadEntityBundleForm($bundle_id);
$this
->assertSession()
->fieldValueEquals('rh_override', $override);
$this
->assertSession()
->checkboxChecked($this
->getOptionId($action));
}
public function testBundleFormFirstSave() {
$test_bundle_id = $this
->createEntityBundle();
$this
->loadEntityBundleForm($test_bundle_id);
$override = BehaviorSettings::OVERRIDE_DISALLOW;
$action = 'access_denied';
$this
->submitForm([
'rh_override' => $override,
'rh_action' => $action,
], 'edit-submit');
$saved_config = $this->behaviorSettingsManager
->loadBehaviorSettingsAsConfig($this->bundleEntityTypeName, $test_bundle_id);
$this
->assertEquals($action, $saved_config
->get('action'));
$this
->assertEquals($override, $saved_config
->get('allow_override'));
}
public function testAllowOverrideValue() {
$bundle_allow = $this
->createEntityBundle();
$this->behaviorSettingsManager
->saveBehaviorSettings([
'action' => 'access_denied',
'allow_override' => BehaviorSettings::OVERRIDE_ALLOW,
'redirect_code' => BehaviorSettings::REDIRECT_NOT_APPLICABLE,
], $this->bundleEntityTypeName, $bundle_allow);
$this
->loadCreateEntityForm();
$this
->assertRabbitHoleSettings();
$bundle_disallow = $this
->createEntityBundle();
$this->behaviorSettingsManager
->saveBehaviorSettings([
'action' => 'access_denied',
'allow_override' => BehaviorSettings::OVERRIDE_DISALLOW,
'redirect_code' => BehaviorSettings::REDIRECT_NOT_APPLICABLE,
], $this->bundleEntityTypeName, $bundle_disallow);
$this
->loadCreateEntityForm();
$this
->assertNoRabbitHoleSettings();
}
public function testBundleFormExistingBehavior() {
$action = 'page_not_found';
$override = BehaviorSettings::OVERRIDE_DISALLOW;
$test_bundle_id = $this
->createEntityBundle();
$this->behaviorSettingsManager
->saveBehaviorSettings([
'action' => $action,
'allow_override' => $override,
'redirect_code' => BehaviorSettings::REDIRECT_NOT_APPLICABLE,
], $this->bundleEntityTypeName, $test_bundle_id);
$this
->loadEntityBundleForm($test_bundle_id);
$this
->assertSession()
->fieldValueEquals('rh_override', $override);
$this
->assertSession()
->checkboxChecked($this
->getOptionId($action));
}
public function testBundleFormSave() {
$test_bundle_id = $this
->createEntityBundle();
$this->behaviorSettingsManager
->saveBehaviorSettings([
'action' => 'access_denied',
'allow_override' => BehaviorSettings::OVERRIDE_DISALLOW,
'redirect_code' => BehaviorSettings::REDIRECT_NOT_APPLICABLE,
], $this->bundleEntityTypeName, $test_bundle_id);
$this
->loadEntityBundleForm($test_bundle_id);
$action = 'page_not_found';
$override = BehaviorSettings::OVERRIDE_ALLOW;
$this
->submitForm([
'rh_override' => $override,
'rh_action' => $action,
], 'edit-submit');
$saved_config = $this->behaviorSettingsManager
->loadBehaviorSettingsAsConfig($this->bundleEntityTypeName, $test_bundle_id);
$this
->assertEquals($action, $saved_config
->get('action'));
$this
->assertEquals($override, $saved_config
->get('allow_override'));
}
public function testExistingEntityNoConfigSave() {
$this
->createEntityBundle();
$entity_id = $this
->createEntity();
$this
->loadEditEntityForm($entity_id);
$action = 'access_denied';
$this
->submitForm([
'rh_action' => $action,
], 'Save');
$entity = $this
->loadEntity($entity_id);
$this
->assertEquals($action, $entity
->get('rh_action')->value);
}
public function testExistingEntitySave() {
$this
->createEntityBundle();
$entity_id = $this
->createEntity('display_page');
$this
->loadEditEntityForm($entity_id);
$action = 'access_denied';
$this
->submitForm([
'rh_action' => $action,
], 'Save');
$this
->assertSession()
->statusCodeEquals(200);
$entity = $this
->loadEntity($entity_id);
$this
->assertEquals($action, $entity
->get('rh_action')->value);
}
public function testDefaultEntitySettingsLoad() {
$this
->createEntityBundle();
$this
->loadCreateEntityForm();
$this
->assertRabbitHoleSettings();
$this
->assertSession()
->checkboxChecked($this
->getOptionId(static::DEFAULT_ACTION));
}
public function testExistingEntitySettingsLoad() {
$this
->createEntityBundle();
$action = 'access_denied';
$entity_id = $this
->createEntity($action);
$this
->loadEditEntityForm($entity_id);
$this
->assertSession()
->checkboxChecked($this
->getOptionId($action));
}
public function testEntityFormSaveRedirect() {
$override = BehaviorSettings::OVERRIDE_DISALLOW;
$action = 'access_denied';
$this
->createEntityBundleFormSubmit($action, $override);
$this
->loadCreateEntityForm();
$this
->assertNoRabbitHoleSettings();
$this
->submitForm([], $this
->getEntityFormSubmit());
$this
->assertSession()
->statusCodeEquals(200);
}
protected function assertRabbitHoleSettings() {
$this
->assertSession()
->fieldExists('rh_action');
$this
->assertSession()
->fieldExists('edit-rh-action-access-denied');
$this
->assertSession()
->fieldExists('edit-rh-action-display-page');
$this
->assertSession()
->fieldExists('edit-rh-action-page-not-found');
$this
->assertSession()
->fieldExists('edit-rh-action-page-redirect');
}
protected function assertNoRabbitHoleSettings() {
$this
->assertSession()
->fieldNotExists('rh_action');
$this
->assertSession()
->fieldNotExists('edit-rh-action-access-denied');
$this
->assertSession()
->fieldNotExists('edit-rh-action-display-page');
$this
->assertSession()
->fieldNotExists('edit-rh-action-page-not-found');
$this
->assertSession()
->fieldNotExists('edit-rh-action-page-redirect');
}
protected function loadEntityBundleForm($bundle) {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet($this
->getEditBundleUrl($bundle));
$this
->assertSession()
->statusCodeEquals(200);
}
protected function loadCreateEntityForm() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet($this
->getCreateEntityUrl());
$this
->assertSession()
->statusCodeEquals(200);
}
protected function loadEditEntityForm($entity_id) {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet($this
->getEditEntityUrl($entity_id));
$this
->assertSession()
->statusCodeEquals(200);
}
protected function loadEntity($id) {
$storage = \Drupal::entityTypeManager()
->getStorage($this->entityType);
$storage
->resetCache([
$id,
]);
return $storage
->load($id);
}
protected function loadBundle($id) {
$storage = \Drupal::entityTypeManager()
->getStorage($this->bundleEntityTypeName);
$storage
->resetCache([
$id,
]);
$bundle = $storage
->load($id);
$this
->assertInstanceOf(ConfigEntityBundleBase::class, $bundle);
return $bundle;
}
protected function getOptionId($action) {
return 'edit-rh-action-' . str_replace('_', '-', $action);
}
protected function getEntityFormSubmit() {
return 'edit-submit';
}
protected abstract function getEditBundleUrl($bundle);
protected abstract function getCreateEntityUrl();
protected abstract function createEntityBundle();
protected abstract function createEntityBundleFormSubmit($action, $override);
protected abstract function createEntity($action = NULL);
protected abstract function getAdminPermissions();
}