Skip to content

Commit

Permalink
Rewrite logic so we don't need to patch Rabbit Hole
Browse files Browse the repository at this point in the history
  • Loading branch information
juampynr committed Apr 16, 2018
1 parent 124615c commit 15677b6
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 32 deletions.
14 changes: 12 additions & 2 deletions entity_is_public.module
Expand Up @@ -5,6 +5,7 @@ use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Session\AnonymousUserSession;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;

/**
Expand Down Expand Up @@ -130,8 +131,17 @@ function path_entity_is_public($entity_type, $entity, array $options, array $dat
* @TODO Refactor \Drupal\rabbit_hole\BehaviorInvoker::processEntity() so it provides this as part of the API.
*/
function rabbit_hole_entity_is_public($entity_type, EntityInterface $entity, array $options, array $data) {
$values = \Drupal::service('rabbit_hole.behavior_invoker')->getRabbitHoleValuesForEntity($entity);
return $values['rh_action'] == 'display_page';
/** @var \Drupal\rabbit_hole\BehaviorInvoker $rabbit_hole_behavior_invoker */
$rabbit_hole_behavior_invoker = \Drupal::service('rabbit_hole.behavior_invoker');
/** @var \Symfony\Component\HttpFoundation\Response $response */
$response = $rabbit_hole_behavior_invoker->processEntity($entity);
$response_unchanged = NULL;
if ($response == $response_unchanged) {
return TRUE;
}
else {
return ($response->getStatusCode() >= 200) && ($response->getStatusCode() < 400);
}
}

/**
Expand Down
159 changes: 129 additions & 30 deletions tests/src/Functional/RabbitHoleEntityIsPublicTest.php
Expand Up @@ -5,6 +5,8 @@
use Drupal\rabbit_hole\Entity\BehaviorSettings;
use Drupal\Tests\BrowserTestBase;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* Test the Rabbit hole integration.
Expand All @@ -18,58 +20,155 @@ class RabbitHoleEntityIsPublicTest extends BrowserTestBase {
*/
public static $modules = ['entity_is_public', 'node', 'rabbit_hole', 'rh_node'];

public function testNodeIsPublic() {
$node_type = $this->drupalCreateContentType();
$node = $this->drupalCreateNode([
'type' => $node_type->getEntityTypeId(),
/**
* @var \Drupal\node\Entity\NodeType
*
* A node type instance.
*/
protected $nodeType;

/**
* @var \Drupal\node\Entity\Node
*
* A node instance.
*/
protected $node;

/**
* @inheritDoc
*/
protected function setUp() {
parent::setUp();

$this->nodeType = $this->drupalCreateContentType();
$this->node = $this->drupalCreateNode([
'type' => $this->nodeType->getEntityTypeId(),
'status' => 1,
]);
$this->assertTrue(entity_is_public('node', $node));
}

public function testDefaultSettings() {
$this->assertTrue(entity_is_public('node', $this->node));
}

public function testContentTypeAllow() {
$rabbit_hole_behavior = BehaviorSettings::create([
'status' => TRUE,
'id' => 'node_type_' . $this->nodeType->getEntityTypeId(),
'action' => 'display_page',
]);
$rabbit_hole_behavior->save();
$this->assertTrue(entity_is_public('node', $this->node));
}

public function testAccessDenied() {
$rabbit_hole_behavior = BehaviorSettings::create([
'status' => TRUE,
'id' => 'node_type_' . $node_type->getEntityTypeId(),
'id' => 'node_type_' . $this->nodeType->getEntityTypeId(),
'action' => 'access_denied',
'redirect_code' => Response::HTTP_MOVED_PERMANENTLY,
]);
$rabbit_hole_behavior->save();
$this->assertFalse(entity_is_public('node', $node));
$this->expectException(AccessDeniedHttpException::class);
entity_is_public('node', $this->node);
}

public function testPageNotFound() {
$rabbit_hole_behavior = BehaviorSettings::create([
'status' => TRUE,
'id' => 'node_type_' . $this->nodeType->getEntityTypeId(),
'action' => 'page_not_found',
'redirect_code' => Response::HTTP_MOVED_PERMANENTLY,
]);
$rabbit_hole_behavior->save();
$this->setExpectedException(NotFoundHttpException::class);
entity_is_public('node', $this->node);
}

$rabbit_hole_behavior->setAction('page_not_found');
public function testPageRedirect() {
$rabbit_hole_behavior = BehaviorSettings::create([
'status' => TRUE,
'id' => 'node_type_' . $this->nodeType->getEntityTypeId(),
'action' => 'page_redirect',
'redirect_code' => Response::HTTP_MOVED_PERMANENTLY,
'redirect' => '<front>'
]);
$rabbit_hole_behavior->save();
$this->assertFalse(entity_is_public('node', $node));
$this->assertTrue(entity_is_public('node', $this->node));
}

$rabbit_hole_behavior->setAction('page_redirect');
public function testOverrideDefault() {
$rabbit_hole_behavior = BehaviorSettings::create([
'status' => TRUE,
'id' => 'node_type_' . $this->nodeType->getEntityTypeId(),
'action' => 'display_page',
'allow_override' => TRUE,
]);
$rabbit_hole_behavior->save();
$this->assertFalse(entity_is_public('node', $node));

$rabbit_hole_behavior->setAction('display_page');
$this->node->rh_action->setValue('bundle_default');
$this->node->save();
$this->assertTrue(entity_is_public('node', $this->node));
}

public function testOverrideAccessDenied() {
$rabbit_hole_behavior = BehaviorSettings::create([
'status' => TRUE,
'id' => 'node_type_' . $this->nodeType->getEntityTypeId(),
'action' => 'display_page',
'allow_override' => TRUE,
]);
$rabbit_hole_behavior->save();
$this->assertTrue(entity_is_public('node', $node));

// Allow individual node overrides.
$rabbit_hole_behavior->setAllowOverride(TRUE);
$this->node->rh_action->setValue('access_denied');
$this->node->save();
$this->expectException(AccessDeniedHttpException::class);
entity_is_public('node', $this->node);
}

public function testOverridePageNotFound() {
$rabbit_hole_behavior = BehaviorSettings::create([
'status' => TRUE,
'id' => 'node_type_' . $this->nodeType->getEntityTypeId(),
'action' => 'display_page',
'allow_override' => TRUE,
]);
$rabbit_hole_behavior->save();

$node->rh_action->setValue('bundle_default');
$node->save();
$this->assertTrue(entity_is_public('node', $node));
$this->node->rh_action->setValue('page_not_found');
$this->node->save();
$this->expectException(NotFoundHttpException::class);
$this->assertFalse(entity_is_public('node', $this->node));
}

$node->rh_action->setValue('access_denied');
$node->save();
$this->assertFalse(entity_is_public('node', $node));
public function testOverridePageRedirect() {
$rabbit_hole_behavior = BehaviorSettings::create([
'status' => TRUE,
'id' => 'node_type_' . $this->nodeType->getEntityTypeId(),
'action' => 'access_denied',
'allow_override' => TRUE,
]);
$rabbit_hole_behavior->save();

$node->rh_action->setValue('page_not_found');
$node->save();
$this->assertFalse(entity_is_public('node', $node));
$this->node->rh_action->setValue('page_redirect');
$this->node->rh_redirect->setValue('<front>');
$this->node->rh_redirect_response->setValue(Response::HTTP_MOVED_PERMANENTLY);
$this->node->save();
$this->assertTrue(entity_is_public('node', $this->node));
}

$node->rh_action->setValue('page_redirect');
$node->save();
$this->assertFalse(entity_is_public('node', $node));
public function testOverrideDisplayPage() {
$rabbit_hole_behavior = BehaviorSettings::create([
'status' => TRUE,
'id' => 'node_type_' . $this->nodeType->getEntityTypeId(),
'action' => 'access_denied',
'allow_override' => TRUE,
]);
$rabbit_hole_behavior->save();

$node->rh_action->setValue('display_page');
$node->save();
$this->assertTrue(entity_is_public('node', $node));
$this->node->rh_action->setValue('display_page');
$this->node->save();
$this->assertTrue(entity_is_public('node', $this->node));
}

}

0 comments on commit 15677b6

Please sign in to comment.