public
Created

  • Download Gist
entity_access_test.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
<?php
 
namespace Drupal\Core\Routing;
 
use Drupal\Core\Access\AccessCheckInterface;
use Drupal\Core\Entity\EntityInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Tests\UnitTestCase;
 
 
 
class EntityAccessCheck implements AccessCheckInterface {
 
/**
* Implements \Drupal\Core\Access\AccessCheckInterface::applies().
*/
public function applies(Route $route) {
return array_key_exists('_access_entity', $route->getRequirements());
}
 
/**
* Implements \Drupal\Core\Access\AccessCheckInterface::access().
*
* The value of the '_access_entity' key must be in the pattern
* 'slug.operation.' The slug corresponds to the named param {slug}.
* This will check a node for 'update' access:
* @code
* pattern: '/foo/{node}/bar'
* requirements:
* _access_entity: 'node.update'
* @endcode
*/
public function access(Route $route, Request $request) {
$requirements = $route->getRequirements();
// Split up the slug and the operation.
list($slug, $operation) = explode('.', $requirements['_access_entity']);
// If this slug is in the request, and it is an entity, check its access.
if ($request->attributes->has($slug)) {
$entity = $request->attributes->get($slug);
if ($entity instanceof EntityInterface) {
return $entity->access($operation);
}
}
}
}
 
class EntityAccessCheckTest extends UnitTestCase {
function testApplies() {
$e = new EntityAccessCheck();
$route = $this->getMockBuilder('Symfony\Component\Routing\Route')
->disableOriginalConstructor()
->getMock();
$route->expects($this->any())
->method('getRequirements')
->will($this->returnValue(array('_access_entity' => '')));
$res = $e->applies($route);
$this->assertEquals(TRUE, $res);
 
$route = $this->getMockBuilder('Symfony\Component\Routing\Route')
->disableOriginalConstructor()
->getMock();
$route->expects($this->any())
->method('getRequirements')
->will($this->returnValue(array()));
$res = $e->applies($route);
$this->assertEquals(FALSE, $res);
 
}
function testAccess() {
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.