Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Policy/OrmResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected function getEntityPolicy(EntityInterface $entity): mixed
$class = $entity::class;
$entityNamespace = '\Model\Entity\\';
$namespace = str_replace('\\', '/', substr($class, 0, (int)strpos($class, $entityNamespace)));
$name = substr($class, strpos($class, $entityNamespace) + strlen($entityNamespace));
$name = str_replace('\\', '/', substr($class, strpos($class, $entityNamespace) + strlen($entityNamespace)));

return $this->findPolicy($class, $name, $namespace);
}
Expand All @@ -119,7 +119,7 @@ protected function getRepositoryPolicy(RepositoryInterface $table): mixed
$class = $table::class;
$tableNamespace = '\Model\Table\\';
$namespace = str_replace('\\', '/', substr($class, 0, (int)strpos($class, $tableNamespace)));
$name = substr($class, strpos($class, $tableNamespace) + strlen($tableNamespace));
$name = str_replace('\\', '/', substr($class, strpos($class, $tableNamespace) + strlen($tableNamespace)));

return $this->findPolicy($class, $name, $namespace);
}
Expand Down
22 changes: 22 additions & 0 deletions tests/TestCase/Policy/OrmResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@
use OverridePlugin\Policy\TagPolicy as OverrideTagPolicy;
use stdClass;
use TestApp\Model\Entity\Article;
use TestApp\Model\Entity\SubDir\Widget;
use TestApp\Model\Table\SubDir\WidgetsTable;
use TestApp\Policy\ArticlePolicy;
use TestApp\Policy\ArticlesTablePolicy;
use TestApp\Policy\SubDir\WidgetPolicy;
use TestApp\Policy\SubDir\WidgetsTablePolicy;
use TestApp\Policy\TestPlugin\BookmarkPolicy;
use TestApp\Service\TestService;
use TestPlugin\Model\Entity\Bookmark;
Expand Down Expand Up @@ -68,6 +72,14 @@ public function testGetPolicyDefinedEntity(): void
$this->assertInstanceOf(ArticlePolicy::class, $policy);
}

public function testGetPolicyDefinedSubDirEntity(): void
{
$widget = new Widget();
$resolver = new OrmResolver('TestApp');
$policy = $resolver->getPolicy($widget);
$this->assertInstanceOf(WidgetPolicy::class, $policy);
}

public function testGetPolicyDefinedPluginEntityAppOveride(): void
{
$bookmark = new Bookmark();
Expand Down Expand Up @@ -108,6 +120,16 @@ public function testGetPolicyDefinedTable(): void
$this->assertInstanceOf(ArticlesTablePolicy::class, $policy);
}

public function testGetPolicyDefinedSubDirTable(): void
{
$widgets = $this->fetchTable('SubDir/Widgets', [
'className' => WidgetsTable::class,
]);
$resolver = new OrmResolver('TestApp');
$policy = $resolver->getPolicy($widgets);
$this->assertInstanceOf(WidgetsTablePolicy::class, $policy);
}

public function testGetPolicyQueryForDefinedTable(): void
{
$articles = $this->fetchTable('Articles');
Expand Down
10 changes: 10 additions & 0 deletions tests/test_app/TestApp/Model/Entity/SubDir/Widget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace TestApp\Model\Entity\SubDir;

use Cake\ORM\Entity;

class Widget extends Entity
{
}
10 changes: 10 additions & 0 deletions tests/test_app/TestApp/Model/Table/SubDir/WidgetsTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace TestApp\Model\Table\SubDir;

use Cake\ORM\Table;

class WidgetsTable extends Table
{
}
8 changes: 8 additions & 0 deletions tests/test_app/TestApp/Policy/SubDir/WidgetPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace TestApp\Policy\SubDir;

class WidgetPolicy
{
}
8 changes: 8 additions & 0 deletions tests/test_app/TestApp/Policy/SubDir/WidgetsTablePolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace TestApp\Policy\SubDir;

class WidgetsTablePolicy
{
}
Loading