View source
<?php
namespace Drupal\Tests\rabbit_hole\Functional;
use Drupal\rabbit_hole\Entity\BehaviorSettings;
use Drupal\rabbit_hole\Exception\InvalidBehaviorSettingException;
use Drupal\Tests\BrowserTestBase;
class RabbitHoleBehaviorSettingsEntityMethodsTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
public static $modules = [
'rabbit_hole',
];
protected $configFactory;
protected function setUp() {
parent::setUp();
$this->configFactory = $this->container
->get('config.factory');
}
public function testCreateEntity() {
$action = 'page_not_found';
$redirect_code = BehaviorSettings::REDIRECT_NOT_APPLICABLE;
$redirect = '/';
$allow_override = BehaviorSettings::OVERRIDE_ALLOW;
$entity = BehaviorSettings::create([
'id' => 'test_behavior_settings',
'action' => $action,
'allow_override' => $allow_override,
'redirect_code' => $redirect_code,
'redirect' => $redirect,
]);
$entity
->save();
$config_entity = $this->configFactory
->get('rabbit_hole.behavior_settings.test_behavior_settings');
$this
->assertEquals($action, $config_entity
->get('action'));
$this
->assertEquals($redirect_code, $config_entity
->get('redirect_code'));
$this
->assertEquals($redirect, $config_entity
->get('redirect'));
$this
->assertEquals($allow_override, $config_entity
->get('allow_override'));
}
public function testSetAction() {
$entity = $this
->createGenericTestEntity();
$action = 'page_not_found';
$entity
->setAction($action);
$this
->assertEquals($action, $entity
->getAction());
}
public function testSetAllowOverride() {
$entity = $this
->createGenericTestEntity();
$this
->behaviorSettingExceptionThrown($entity, 'setAllowOverride', [
'some non-bool value',
], __METHOD__);
$entity
->setAllowOverride(TRUE);
$this
->assertTrue($entity
->getAllowOverride());
$entity
->setAllowOverride(FALSE);
$this
->assertFalse($entity
->getAllowOverride());
}
public function testSetRedirectCode() {
$entity = $this
->createGenericTestEntity();
$entity
->setAction('display_page');
$this
->behaviorSettingExceptionThrown($entity, 'setRedirectCode', [
BehaviorSettings::REDIRECT_FOUND,
], __METHOD__);
$entity
->setAction('redirect');
$this
->behaviorSettingExceptionThrown($entity, 'setRedirectCode', [
209458253,
], __METHOD__);
$entity
->setRedirectCode(BehaviorSettings::REDIRECT_FOUND);
$this
->assertEquals(BehaviorSettings::REDIRECT_FOUND, $entity
->getRedirectCode());
$entity
->setRedirectCode(BehaviorSettings::REDIRECT_MOVED_PERMANENTLY);
$this
->assertEquals(BehaviorSettings::REDIRECT_MOVED_PERMANENTLY, $entity
->getRedirectCode());
}
public function testSetRedirectPath() {
$entity = $this
->createGenericTestEntity();
$entity
->setAction('display_page');
$this
->behaviorSettingExceptionThrown($entity, 'setRedirectPath', [
'/',
], __METHOD__);
$entity
->setAction('redirect');
$path = '/somepage';
$entity
->setRedirectPath($path);
$this
->assertEquals($path, $entity
->getRedirectPath());
}
private function createGenericTestEntity() {
return BehaviorSettings::create([
'id' => 'test_behavior_settings',
'action' => 'access_denied',
'redirect_code' => BehaviorSettings::REDIRECT_NOT_APPLICABLE,
'redirect' => NULL,
]);
}
private function behaviorSettingExceptionThrown(BehaviorSettings $entity, $method, array $args, $parent) {
$exception_was_thrown = FALSE;
try {
call_user_func([
$entity,
$method,
], $args);
} catch (InvalidBehaviorSettingException $ex) {
$exception_was_thrown = TRUE;
}
$this
->assertTrue($exception_was_thrown, 'Exception thrown executing ' . $method . ', called from ' . $parent);
}
}