Skip to content

Commit

Permalink
Merge pull request #297 from cakephp/3.next
Browse files Browse the repository at this point in the history
3.next
  • Loading branch information
ADmad authored Oct 18, 2024
2 parents f0d9cbd + b5c3b87 commit eeacf4f
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 109 deletions.
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
},
"require-dev": {
"cakephp/authentication": "^3.0",
"cakephp/bake": "^3.0",
"cakephp/cakephp": "^5.0",
"cakephp/bake": "dev-3.next",
"cakephp/cakephp": "dev-5.next as 5.1.0",
"cakephp/cakephp-codesniffer": "^5.0",
"phpunit/phpunit": "^10.1.0"
"phpunit/phpunit": "^10.5.5 || ^11.1.3"
},
"suggest": {
"cakephp/http": "To use \"RequestPolicyInterface\" (Not needed separately if using full CakePHP framework).",
Expand Down Expand Up @@ -80,5 +80,7 @@
"stan-setup": "phive install",
"test": "phpunit",
"test-coverage": "phpunit --coverage-clover=clover.xml"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
175 changes: 80 additions & 95 deletions tests/TestCase/AuthorizationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@

use Authorization\AuthorizationService;
use Authorization\IdentityDecorator;
use Authorization\IdentityInterface;
use Authorization\Policy\BeforePolicyInterface;
use Authorization\Policy\BeforeScopeInterface;
use Authorization\Policy\Exception\MissingMethodException;
use Authorization\Policy\MapResolver;
use Authorization\Policy\OrmResolver;
use Authorization\Policy\Result;
use Authorization\Policy\ResultInterface;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\QueryInterface;
use Cake\TestSuite\TestCase;
use PHPUnit\Framework\ExpectationFailedException;
use TestApp\Model\Entity\Article;
use TestApp\Model\Table\ArticlesTable;
use TestApp\Policy\ArticlePolicy;
Expand Down Expand Up @@ -317,24 +320,22 @@ public function testApplyScopeAdditionalArguments()
public function testBeforeFalse()
{
$entity = new Article();

$policy = $this->getMockBuilder(BeforePolicyInterface::class)
->onlyMethods(['before'])
->addMethods(['canAdd'])
->getMock();

$policy->expects($this->once())
->method('before')
->with($this->isInstanceOf(IdentityDecorator::class), $entity, 'add')
->willReturn(false);
$policy = new class implements BeforePolicyInterface {
public function before($identity, $resource, $action): bool|ResultInterface|null
{
return false;
}

public function canAdd($user, $entity)
{
throw new ExpectationFailedException('This method should not be called');
}
};

$resolver = new MapResolver([
Article::class => $policy,
]);

$policy->expects($this->never())
->method('canAdd');

$service = new AuthorizationService($resolver);

$user = new IdentityDecorator($service, [
Expand All @@ -348,19 +349,17 @@ public function testBeforeFalse()
public function testBeforeTrue()
{
$entity = new Article();

$policy = $this->getMockBuilder(BeforePolicyInterface::class)
->onlyMethods(['before'])
->addMethods(['canAdd'])
->getMock();

$policy->expects($this->once())
->method('before')
->with($this->isInstanceOf(IdentityDecorator::class), $entity, 'add')
->willReturn(true);

$policy->expects($this->never())
->method('canAdd');
$policy = new class implements BeforePolicyInterface {
public function before($identity, $resource, $action): bool|ResultInterface|null
{
return true;
}

public function canAdd($user, $entity)
{
throw new ExpectationFailedException('This method should not be called');
}
};

$resolver = new MapResolver([
Article::class => $policy,
Expand All @@ -379,21 +378,17 @@ public function testBeforeTrue()
public function testBeforeNull()
{
$entity = new Article();

$policy = $this->getMockBuilder(BeforePolicyInterface::class)
->onlyMethods(['before'])
->addMethods(['canAdd'])
->getMock();

$policy->expects($this->once())
->method('before')
->with($this->isInstanceOf(IdentityDecorator::class), $entity, 'add')
->willReturn(null);

$policy->expects($this->once())
->method('canAdd')
->with($this->isInstanceOf(IdentityDecorator::class), $entity)
->willReturn(true);
$policy = new class implements BeforePolicyInterface {
public function before($identity, $resource, $action): bool|ResultInterface|null
{
return null;
}

public function canAdd($user, $entity): bool
{
return true;
}
};

$resolver = new MapResolver([
Article::class => $policy,
Expand All @@ -412,19 +407,17 @@ public function testBeforeNull()
public function testBeforeResultTrue()
{
$entity = new Article();

$policy = $this->getMockBuilder(BeforePolicyInterface::class)
->onlyMethods(['before'])
->addMethods(['canAdd'])
->getMock();

$policy->expects($this->once())
->method('before')
->with($this->isInstanceOf(IdentityDecorator::class), $entity, 'add')
->willReturn(new Result(true));

$policy->expects($this->never())
->method('canAdd');
$policy = new class implements BeforePolicyInterface {
public function before($identity, $resource, $action): bool|ResultInterface|null
{
return new Result(true);
}

public function canAdd($user, $entity)
{
throw new ExpectationFailedException('This method should not be called');
}
};

$resolver = new MapResolver([
Article::class => $policy,
Expand All @@ -443,19 +436,17 @@ public function testBeforeResultTrue()
public function testBeforeResultFalse()
{
$entity = new Article();

$policy = $this->getMockBuilder(BeforePolicyInterface::class)
->onlyMethods(['before'])
->addMethods(['canAdd'])
->getMock();

$policy->expects($this->once())
->method('before')
->with($this->isInstanceOf(IdentityDecorator::class), $entity, 'add')
->willReturn(new Result(false));

$policy->expects($this->never())
->method('canAdd');
$policy = new class implements BeforePolicyInterface {
public function before($identity, $resource, $action): bool|ResultInterface|null
{
return new Result(false);
}

public function canAdd($user, $entity)
{
throw new ExpectationFailedException('This method should not be called');
}
};

$resolver = new MapResolver([
Article::class => $policy,
Expand All @@ -474,19 +465,17 @@ public function testBeforeResultFalse()
public function testBeforeScopeNonNull()
{
$entity = new Article();

$policy = $this->getMockBuilder(BeforeScopeInterface::class)
->onlyMethods(['beforeScope'])
->addMethods(['scopeIndex'])
->getMock();

$policy->expects($this->once())
->method('beforeScope')
->with($this->isInstanceOf(IdentityDecorator::class), $entity, 'index')
->willReturn('foo');

$policy->expects($this->never())
->method('scopeIndex');
$policy = new class implements BeforeScopeInterface {
public function beforeScope(?IdentityInterface $identity, mixed $resource, string $action): mixed
{
return 'foo';
}

public function scopeIndex(IdentityInterface $user, QueryInterface $query)
{
throw new ExpectationFailedException('This method should not be called');
}
};

$resolver = new MapResolver([
Article::class => $policy,
Expand All @@ -505,21 +494,17 @@ public function testBeforeScopeNonNull()
public function testBeforeScopeNull()
{
$entity = new Article();

$policy = $this->getMockBuilder(BeforeScopeInterface::class)
->onlyMethods(['beforeScope'])
->addMethods(['scopeIndex'])
->getMock();

$policy->expects($this->once())
->method('beforeScope')
->with($this->isInstanceOf(IdentityDecorator::class), $entity, 'index')
->willReturn(null);

$policy->expects($this->once())
->method('scopeIndex')
->with($this->isInstanceOf(IdentityDecorator::class), $entity)
->willReturn('bar');
$policy = new class implements BeforeScopeInterface {
public function beforeScope(?IdentityInterface $identity, mixed $resource, string $action): mixed
{
return null;
}

public function scopeIndex(IdentityInterface $user, EntityInterface $entity)
{
return 'bar';
}
};

$resolver = new MapResolver([
Article::class => $policy,
Expand Down
11 changes: 5 additions & 6 deletions tests/TestCase/IdentityDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Authorization\IdentityDecorator;
use BadMethodCallException;
use Cake\TestSuite\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use stdClass;
use TestApp\Model\Entity\Article;

Expand All @@ -31,9 +32,7 @@ public static function constructorDataProvider()
];
}

/**
* @dataProvider constructorDataProvider
*/
#[DataProvider('constructorDataProvider')]
public function testConstructorAccepted($data)
{
$auth = $this->createMock(AuthorizationServiceInterface::class);
Expand All @@ -50,7 +49,7 @@ public function testCanDelegation()
$auth->expects($this->once())
->method('can')
->with($identity, 'update', $resource)
->will($this->returnValue(true));
->willReturn(true);
$this->assertTrue($identity->can('update', $resource));
}

Expand All @@ -63,7 +62,7 @@ public function testApplyScopeDelegation()
$auth->expects($this->once())
->method('applyScope')
->with($identity, 'update', $resource)
->will($this->returnValue(true));
->willReturn(true);
$this->assertTrue($identity->applyScope('update', $resource));
}

Expand All @@ -76,7 +75,7 @@ public function testApplyScopeAdditionalParams()
$auth->expects($this->once())
->method('applyScope')
->with($identity, 'update', $resource, 'first argument', false)
->will($this->returnValue(true));
->willReturn(true);
$this->assertTrue($identity->applyScope('update', $resource, 'first argument', false));
}

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Middleware/AuthorizationMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testInvokeAuthorizationRequiredError()
$service = $this->createMock(AuthorizationServiceInterface::class);
$service->expects($this->once())
->method('authorizationChecked')
->will($this->returnValue(false));
->willReturn(false);

$request = (new ServerRequest())->withAttribute('identity', ['id' => 1]);
$handler = new TestRequestHandler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Cake\Core\Configure;
use Cake\Http\ServerRequestFactory;
use Cake\TestSuite\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;

class RedirectHandlerTest extends TestCase
{
Expand Down Expand Up @@ -100,9 +101,7 @@ public static function httpMethodProvider()
];
}

/**
* @dataProvider httpMethodProvider
*/
#[DataProvider('httpMethodProvider')]
public function testHandleRedirectionIgnoreNonIdempotentMethods($method)
{
$handler = new RedirectHandler();
Expand Down

0 comments on commit eeacf4f

Please sign in to comment.