Skip to content

Commit

Permalink
Protected fields afterDelete fix (#482)
Browse files Browse the repository at this point in the history
protected fields afterDelete fix
  • Loading branch information
jorisvaesen committed Mar 18, 2018
1 parent 67338ef commit d640151
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ matrix:

before_script:
- composer self-update
- composer install --prefer-source --no-interaction --dev
- composer install --prefer-source --no-interaction

- sh -c "if [ '$DB' = 'mysql' ]; then if [ '$DOCKER' = '1' ]; then apt-get -qq install -qq -y mysql-server && service mysql start; fi; mysql -e 'CREATE DATABASE cakephp_test;'; fi"

- sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi"

- sh -c "if [ '$PHPCS' = '1' ]; then composer require 'cakephp/cakephp-codesniffer:dev-master'; fi"

- sh -c "if [ '$COVERALLS' = '1' ]; then composer require --dev satooshi/php-coveralls:dev-master; fi"
- sh -c "if [ '$COVERALLS' = '1' ]; then composer require --dev satooshi/php-coveralls; fi"
- sh -c "if [ '$COVERALLS' = '1' ]; then mkdir -p build/logs; fi"

- command -v phpenv > /dev/null && phpenv rehash || true

script:
- sh -c "if [ '$COVERALLS' = '1' ]; then vendor/bin/phpunit --stderr --coverage-clover build/logs/clover.xml; fi"
- sh -c "if [ '$COVERALLS' = '1' ]; then php vendor/bin/coveralls -v; fi"
- sh -c "if [ '$COVERALLS' = '1' ]; then php vendor/bin/php-coveralls -v; fi"
- sh -c "if [ '$DEFAULT' = '1' ]; then vendor/bin/phpunit --stderr; fi"
- sh -c "if [ '$PHPCS' = '1' ]; then vendor/bin/phpcs -n -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi"

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"phpunit/phpunit": "<6.0",
"league/flysystem-vfs": "*",
"cakephp/cakephp-codesniffer": "dev-master",
"satooshi/php-coveralls": "dev-master"
"satooshi/php-coveralls": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Behavior/UploadBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function afterDelete(Event $event, Entity $entity, ArrayObject $options)
$result = true;

foreach ($this->config() as $field => $settings) {
if (Hash::get($settings, 'keepFilesOnDelete', true)) {
if (in_array($field, $this->protectedFieldNames) || Hash::get($settings, 'keepFilesOnDelete', true)) {
continue;
}

Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/Model/Behavior/UploadBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,27 @@ public function testAfterDeleteUsesDeleteCallback()
$behavior->afterDelete(new Event('fake.event'), $this->entity, new ArrayObject);
}

public function testAfterDeleteWithProtectedFieldName()
{
$settings = $this->settings;
$settings['priority'] = 11;

$methods = array_diff($this->behaviorMethods, ['afterDelete', 'config', 'setConfig', 'getConfig']);
$behavior = $this->getMockBuilder('Josegonzalez\Upload\Model\Behavior\UploadBehavior')
->setMethods($methods)
->setConstructorArgs([$this->table, $settings])
->getMock();

$behavior->expects($this->any())
->method('getWriter')
->will($this->returnValue($this->writer));
$this->writer->expects($this->any())
->method('delete')
->will($this->returnValue([true]));

$this->assertTrue($behavior->afterDelete(new Event('fake.event'), $this->entity, new ArrayObject));
}

public function testGetWriter()
{
$processor = $this->behavior->getWriter($this->entity, [], 'field', []);
Expand Down

0 comments on commit d640151

Please sign in to comment.