Skip to content
Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"require-dev": {
"cakephp/cakephp": "^5.0.0",
"phpunit/phpunit": "^10.1.0",
"phpunit/phpunit": "^10.5.58 || ^11.5.3 || ^12.4",
"cakephp/cakephp-codesniffer": "^5.0"
},
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0"?>
<ruleset name="Trash">
<config name="installed_paths" value="../../cakephp/cakephp-codesniffer" />
<file>src/</file>
<file>tests/</file>

<rule ref="CakePHP" />
</ruleset>
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ parameters:
paths:
- src/
ignoreErrors:
- '#Call to an undefined method Cake\\ORM\\Table::cascadingRestoreTrash\(\)#'
- identifier: missingType.iterableValue
- identifier: missingType.generics
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<coverage/>
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
<env name="FIXTURE_SCHEMA_METADATA" value="./tests/schema.php"/>
</php>
<!-- Add any additional test suites you want to run here -->
Expand Down
3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@

<DocblockTypeContradiction errorLevel="info" />
<RedundantConditionGivenDocblockType errorLevel="info" />

<ClassMustBeFinal errorLevel="info" />
<MissingOverrideAttribute errorLevel="info" />
</issueHandlers>
</psalm>
35 changes: 25 additions & 10 deletions src/Model/Behavior/TrashBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@ public function trash(EntityInterface $entity, array $options = []): bool
}
}

$entity->patch([$this->getTrashField(false) => new DateTime()]);
/** @phpstan-ignore function.alreadyNarrowedType */
if (method_exists($entity, 'patch')) {
$entity->patch([$this->getTrashField(false) => new DateTime()]);
} else {
$entity->set($this->getTrashField(false), new DateTime());
}

return (bool)$this->_table->save($entity, $options);
}
Expand Down Expand Up @@ -258,7 +263,7 @@ public function trashAll(mixed $conditions): int
{
return $this->_table->updateAll(
[$this->getTrashField(false) => new DateTime()],
$conditions
$conditions,
);
}

Expand Down Expand Up @@ -287,7 +292,13 @@ public function restoreTrash(?EntityInterface $entity = null, array $options = [
if ($entity->isDirty()) {
throw new CakeException('Can not restore from a dirty entity.');
}
$entity->patch($data, ['guard' => false]);

/** @phpstan-ignore function.alreadyNarrowedType */
if (method_exists($entity, 'patch')) {
$entity->patch($data, ['guard' => false]);
} else {
$entity->set($data, ['guard' => false]);
}

return $this->_table->save($entity, $options);
}
Expand All @@ -304,18 +315,21 @@ public function restoreTrash(?EntityInterface $entity = null, array $options = [
*/
public function cascadingRestoreTrash(
?EntityInterface $entity = null,
array $options = []
array $options = [],
): bool|int|EntityInterface {
$result = $this->restoreTrash($entity, $options);
$return = $result;

$associations = $this->_table->associations()->getByType(['HasOne', 'HasMany']);
foreach ($associations as $association) {
if ($this->_isRecursable($association, $this->_table)) {
if ($entity === null) {
if ($result > 1) {
if ($result > 0) {
/** @var \Muffin\Trash\Model\Behavior\TrashBehavior $behavior */
$behavior = $association->getTarget()->getBehavior('Trash');
$result += $behavior->cascadingRestoreTrash(null, $options);
if ($behavior->cascadingRestoreTrash(null, $options) === false) {
$return = false;
}
}
} else {
/** @var list<string> $foreignKey */
Expand All @@ -324,20 +338,21 @@ public function cascadingRestoreTrash(
$bindingKey = (array)$association->getBindingKey();
$conditions = array_combine($foreignKey, $entity->extract($bindingKey));

foreach ($association->find('withTrashed')->where($conditions) as $related) {
/** @var \Cake\Datasource\EntityInterface $related */
foreach ($association->find('withTrashed')->where($conditions)->all() as $related) {
/** @var \Muffin\Trash\Model\Behavior\TrashBehavior $behavior */
$behavior = $association->getTarget()->getBehavior('Trash');
if (
!$behavior->cascadingRestoreTrash($related, ['_primary' => false] + $options)
$behavior->cascadingRestoreTrash($related, ['_primary' => false] + $options) === false
) {
$result = false;
$return = false;
}
}
}
}
}

return $result;
return $return;
}

/**
Expand Down
46 changes: 24 additions & 22 deletions tests/TestCase/Model/Behavior/TrashBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Cake\TestSuite\TestCase;
use InvalidArgumentException;
use Muffin\Trash\Model\Behavior\TrashBehavior;
use PHPUnit\Framework\Attributes\DataProvider;

class TrashBehaviorTest extends TestCase
{
Expand Down Expand Up @@ -58,7 +59,7 @@ public function setUp(): void

$this->CompositeArticlesUsers = $this->getTableLocator()->get(
'Muffin/Trash.CompositeArticlesUsers',
['table' => 'trash_composite_articles_users']
['table' => 'trash_composite_articles_users'],
);
$this->CompositeArticlesUsers->addBehavior('Muffin/Trash.Trash');

Expand Down Expand Up @@ -129,7 +130,7 @@ public function testBeforeFindWithTrashFieldInComparison()
$query = $this->Articles->find('all');

$result = $query->where(
[$this->Articles->aliasField('trashed') . ' >= ' => new DateTime('-1 day')]
[$this->Articles->aliasField('trashed') . ' >= ' => new DateTime('-1 day')],
)->toArray();
$this->assertCount(2, $result);
}
Expand Down Expand Up @@ -179,7 +180,7 @@ function (Event $event, EntityInterface $entity, ArrayObject $options) {
$entity->setError('id', 'Save aborted');
$event->setResult(false);
$event->stopPropagation();
}
},
);

$result = $this->Articles->delete($article);
Expand Down Expand Up @@ -223,15 +224,15 @@ function (Event $event, EntityInterface $entity, ArrayObject $options) use (&$ha
if (isset($options['deleteOptions'])) {
$hasDeleteOptionsBefore = true;
}
}
},
);
$this->Comments->getEventManager()->on(
'Model.afterDelete',
function (Event $event, EntityInterface $entity, ArrayObject $options) use (&$hasDeleteOptionsAfter) {
if (isset($options['deleteOptions'])) {
$hasDeleteOptionsAfter = true;
}
}
},
);

$article = $this->Articles->get(1);
Expand Down Expand Up @@ -265,24 +266,24 @@ function (Event $event, EntityInterface $entity, ArrayObject $options) use (&$ma
if (isset($options['deleteOptions'])) {
$mainHasDeleteOptions = true;
}
}
},
);
$this->Comments->getEventManager()->on(
'Model.beforeSave',
function (
Event $event,
EntityInterface $entity,
ArrayObject $options
ArrayObject $options,
) use (
&$dependentHasDeleteOptions,
&$dependentIsNotPrimary
&$dependentIsNotPrimary,
) {
if (isset($options['deleteOptions'])) {
$dependentHasDeleteOptions = true;
}

$dependentIsNotPrimary = $options['_primary'] === false;
}
},
);

$article = $this->Articles->get(1);
Expand Down Expand Up @@ -329,7 +330,7 @@ public function testTrash()
$this->getTableLocator()
->get('ArticlesUsers', ['table' => 'trash_articles_users'])
->find()
->count()
->count(),
);
}

Expand Down Expand Up @@ -595,24 +596,24 @@ function (Event $event, EntityInterface $entity, ArrayObject $options) use (&$ma
if (isset($options['restoreOptions'])) {
$mainHasRestoreOptions = true;
}
}
},
);
$this->Comments->getEventManager()->on(
'Model.beforeSave',
function (
Event $event,
EntityInterface $entity,
ArrayObject $options
ArrayObject $options,
) use (
&$dependentHasRestoreOptions,
&$dependentIsNotPrimary
&$dependentIsNotPrimary,
) {
if (isset($options['restoreOptions'])) {
$dependentHasRestoreOptions = true;
}

$dependentIsNotPrimary = $options['_primary'] === false;
}
},
);

$result = $this->Articles->getBehavior('Trash')->cascadingRestoreTrash($article, [
Expand Down Expand Up @@ -690,7 +691,7 @@ public function testCascadingUntrashEntity()

$this->assertInstanceOf(
EntityInterface::class,
$this->Articles->getBehavior('Trash')->cascadingRestoreTrash($article)
$this->Articles->getBehavior('Trash')->cascadingRestoreTrash($article),
);

$article = $this->Articles
Expand Down Expand Up @@ -766,7 +767,7 @@ public function testCascadingUntrashAll()
$this->assertNotEmpty($article->composite_articles_users[0]->trashed);
$this->assertInstanceOf(DateTime::class, $article->composite_articles_users[0]->trashed);

$this->assertEquals(8, $this->Articles->getBehavior('Trash')->cascadingRestoreTrash());
$this->assertEquals(3, $this->Articles->getBehavior('Trash')->cascadingRestoreTrash());

$article = $this->Articles
->find()
Expand Down Expand Up @@ -903,7 +904,7 @@ public function testGetTrashFieldSchemaIntrospection()
{
$this->assertEquals(
'Articles.trashed',
$this->Articles->behaviors()->get('Trash')->getTrashField()
$this->Articles->behaviors()->get('Trash')->getTrashField(),
);
}

Expand All @@ -923,11 +924,11 @@ public function testImpEInvalidArgumentException()
/**
* Test the implementedEvents method.
*
* @dataProvider provideConfigsForImplementedEventsTest
* @param array $config Initial behavior config.
* @param array $implementedEvents Expected implementedEvents.
* @return void
*/
#[DataProvider('provideConfigsForImplementedEventsTest')]
public function testImplementedEvents(array $config, array $implementedEvents)
{
$trash = new TrashBehavior($this->Users, $config);
Expand All @@ -942,6 +943,9 @@ public function testImplementedEvents(array $config, array $implementedEvents)
*/
public static function provideConfigsForImplementedEventsTest()
{
$callable = function () {
};

return [
'No event config inherits default events' => [
[],
Expand Down Expand Up @@ -1001,8 +1005,7 @@ public static function provideConfigsForImplementedEventsTest()
[
'events' => [
'Model.beforeDelete' => [
'callable' => function () {
},
'callable' => $callable,
],
'Model.beforeFind' => [
'callable' => ['', 'beforeDelete'],
Expand All @@ -1012,8 +1015,7 @@ public static function provideConfigsForImplementedEventsTest()
],
[
'Model.beforeDelete' => [
'callable' => function () {
},
'callable' => $callable,
],
'Model.beforeFind' => [
'callable' => ['', 'beforeDelete'],
Expand Down
Loading