diff --git a/plugins/baser-core/composer.json b/plugins/baser-core/composer.json index 4e574b71a6..e6e80b0e11 100644 --- a/plugins/baser-core/composer.json +++ b/plugins/baser-core/composer.json @@ -16,20 +16,20 @@ "josegonzalez/dotenv": "^3.2", "mobiledetect/mobiledetectlib": "^4.8.03", "ext-json": "*", - "baserproject/bc-admin-third": "^5.1", - "baserproject/bc-blog": "^5.1", - "baserproject/bc-content-link": "^5.1", - "baserproject/bc-custom-content": "^5.1", - "baserproject/bc-editor-template": "^5.1", - "baserproject/bc-favorite": "^5.1", - "baserproject/bc-front": "^5.1", - "baserproject/bc-installer": "^5.1", - "baserproject/bc-mail": "^5.1", - "baserproject/bc-search-index": "^5.1", - "baserproject/bc-theme-config": "^5.1", - "baserproject/bc-theme-file": "^5.1", - "baserproject/bc-uploader": "^5.1", - "baserproject/bc-widget-area": "^5.1", + "baserproject/bc-admin-third": "5.1.x", + "baserproject/bc-blog": "5.1.x", + "baserproject/bc-content-link": "5.1.x", + "baserproject/bc-custom-content": "5.1.x", + "baserproject/bc-editor-template": "5.1.x", + "baserproject/bc-favorite": "5.1.x", + "baserproject/bc-front": "5.1.x", + "baserproject/bc-installer": "5.1.x", + "baserproject/bc-mail": "5.1.x", + "baserproject/bc-search-index": "5.1.x", + "baserproject/bc-theme-config": "5.1.x", + "baserproject/bc-theme-file": "5.1.x", + "baserproject/bc-uploader": "5.1.x", + "baserproject/bc-widget-area": "5.1.x", "ext-mbstring": "*", "ext-zip": "*", "ext-gd": "*" diff --git a/plugins/baser-core/config/update/5.1.0/src/Application.php b/plugins/baser-core/config/update/5.1.0/src/Application.php deleted file mode 100644 index e5d6221948..0000000000 --- a/plugins/baser-core/config/update/5.1.0/src/Application.php +++ /dev/null @@ -1,153 +0,0 @@ - - */ -class Application extends BaseApplication -{ - /** - * Load all the application configuration and bootstrap logic. - * - * @return void - */ - public function bootstrap(): void - { - // Call parent to load bootstrap from files. - parent::bootstrap(); - - if (PHP_SAPI === 'cli') { - if(!BcUtil::is51()) { - $this->bootstrapCli(); - } - } else { - FactoryLocator::add( - 'Table', - (new TableLocator())->allowFallbackClass(false) - ); - } - - if(!BcUtil::is51()) { - /* - * Only try to load DebugKit in development mode - * Debug Kit should not be installed on a production system - */ - if (Configure::read('debug')) { - $this->addPlugin('DebugKit'); - } - - // Load more plugins here - $this->addPlugin('BaserCore'); - } - } - - /** - * Setup the middleware queue your application will use. - * - * @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to setup. - * @return \Cake\Http\MiddlewareQueue The updated middleware queue. - */ - public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue - { - if(BcUtil::is51()) { - $middlewareQueue = $middlewareQueue - // Catch any exceptions in the lower layers, - // and make an error page/response - ->add(new ErrorHandlerMiddleware(Configure::read('Error'), $this)); - } else { - $middlewareQueue = $middlewareQueue - // Catch any exceptions in the lower layers, - // and make an error page/response - ->add(new ErrorHandlerMiddleware(Configure::read('Error'))); - } - - $middlewareQueue - // Handle plugin/theme assets like CakePHP normally does. - ->add(new AssetMiddleware([ - 'cacheTime' => Configure::read('Asset.cacheTime'), - ])) - - // Add routing middleware. - // If you have a large number of routes connected, turning on routes - // caching in production could improve performance. - // See https://github.com/CakeDC/cakephp-cached-routing - ->add(new RoutingMiddleware($this)) - - // Parse various types of encoded request bodies so that they are - // available as array through $request->getData() - // https://book.cakephp.org/5/en/controllers/middleware.html#body-parser-middleware - ->add(new BodyParserMiddleware()) - - // Cross Site Request Forgery (CSRF) Protection Middleware - // https://book.cakephp.org/5/en/security/csrf.html#cross-site-request-forgery-csrf-middleware - ->add(new CsrfProtectionMiddleware([ - 'httponly' => true, - ])); - - return $middlewareQueue; - } - - /** - * Register application container services. - * - * @param \Cake\Core\ContainerInterface $container The Container to update. - * @return void - * @link https://book.cakephp.org/5/en/development/dependency-injection.html#dependency-injection - */ - public function services(ContainerInterface $container): void - { - } - - /** - * Bootstrapping for CLI application. - * - * That is when running commands. - * - * @return void - */ - protected function bootstrapCli(): void - { - try { - $this->addPlugin('Bake'); - } catch (MissingPluginException $e) { - // Do not halt if the plugin is missing - } - - $this->addPlugin('Migrations'); - - // Load more plugins here - } -} diff --git a/plugins/baser-core/src/Error/MissingColumnException.php b/plugins/baser-core/src/Error/MissingColumnException.php new file mode 100644 index 0000000000..74ee835447 --- /dev/null +++ b/plugins/baser-core/src/Error/MissingColumnException.php @@ -0,0 +1,20 @@ + + * Copyright (c) NPO baser foundation + * + * @copyright Copyright (c) NPO baser foundation + * @link https://basercms.net baserCMS Project + * @since 5.0.0 + * @license https://basercms.net/license/index.html MIT License + */ +namespace BaserCore\Error; + +use Cake\Core\Exception\CakeException; + +/** + * MissingColumnException + */ +class MissingColumnException extends CakeException +{ +} diff --git a/plugins/baser-core/src/Model/Table/ContentsTable.php b/plugins/baser-core/src/Model/Table/ContentsTable.php index 55cb9a1c30..3dbf6a77d9 100644 --- a/plugins/baser-core/src/Model/Table/ContentsTable.php +++ b/plugins/baser-core/src/Model/Table/ContentsTable.php @@ -26,7 +26,6 @@ use BaserCore\Model\Entity\Content; use Cake\Datasource\EntityInterface; use Cake\Datasource\ConnectionManager; -use SoftDelete\Model\Table\SoftDeleteTrait; /** * Class ContentsTable diff --git a/plugins/cakephp-soft-delete/src/Model/Table/SoftDeleteTrait.php b/plugins/baser-core/src/Model/Table/SoftDeleteTrait.php similarity index 90% rename from plugins/cakephp-soft-delete/src/Model/Table/SoftDeleteTrait.php rename to plugins/baser-core/src/Model/Table/SoftDeleteTrait.php index 0cc051f6df..c588b1ab6a 100644 --- a/plugins/cakephp-soft-delete/src/Model/Table/SoftDeleteTrait.php +++ b/plugins/baser-core/src/Model/Table/SoftDeleteTrait.php @@ -1,18 +1,31 @@ + * Copyright (c) NPO baser foundation + * + * @copyright Copyright (c) NPO baser foundation + * @link https://basercms.net baserCMS Project + * @since 5.0.0 + * @license https://basercms.net/license/index.html MIT License + */ + +namespace BaserCore\Model\Table; use Cake\ORM\RulesChecker; use Cake\Datasource\EntityInterface; -use SoftDelete\Error\MissingColumnException; -use SoftDelete\ORM\SelectQuery; +use BaserCore\Error\MissingColumnException; +use BaserCore\ORM\SelectQuery; +/** + * SoftDeleteTrait + */ trait SoftDeleteTrait { /** * Get the configured deletion field * * @return string - * @throws \SoftDelete\Error\MissingColumnException + * @throws \BaserCore\Error\MissingColumnException */ public function getSoftDeleteField() { @@ -41,7 +54,7 @@ public function getSoftDeleteField() * Fatal error: Declaration of SoftDelete\Model\Table\SoftDeleteTrait::query(): SoftDelete\ORM\Query must be compatible with Cake\ORM\Table::query(): Cake\ORM\Query * 上記エラー回避のため戻り値の型宣言では実体と異なるがCakePHP本体のquery()で定義されている上書き元の型(\Cake\ORM\Query)を明示する * - * @return \SoftDelete\ORM\SelectQuery + * @return \BaserCore\ORM\SelectQuery */ public function SelectQuery(): SelectQuery { diff --git a/plugins/cakephp-soft-delete/src/ORM/SelectQuery.php b/plugins/baser-core/src/ORM/SelectQuery.php similarity index 97% rename from plugins/cakephp-soft-delete/src/ORM/SelectQuery.php rename to plugins/baser-core/src/ORM/SelectQuery.php index 3387235f01..b4b631974f 100644 --- a/plugins/cakephp-soft-delete/src/ORM/SelectQuery.php +++ b/plugins/baser-core/src/ORM/SelectQuery.php @@ -1,5 +1,5 @@ delete($user); // $user entity is now soft deleted if UsersTable uses SoftDeleteTrait. -``` - -### Restoring Soft deleted records - -To restore a soft deleted entity into an active state, use the `restore` method: - -```php -// in src/Model/Table/UsersTable.php -// Let's suppose $user #1 is soft deleted. -$user = $this->Users->find('all', ['withDeleted'])->where('id', 1)->first(); -$this->restore($user); // $user #1 is now restored. -``` - -### Finding records - -`find`, `get` or dynamic finders (such as `findById`) will only return non soft deleted records. -To also return soft deleted records, `$options` must contain `'withDeleted'`. Example: - -```php -// in src/Model/Table/UsersTable.php -$nonSoftDeletedRecords = $this->find('all'); -$allRecords = $this->find('all', ['withDeleted']); -``` - -### Hard deleting records - -To hard delete a single entity: -```php -// in src/Model/Table/UsersTable.php -$user = $this->get($userId); -$success = $this->hardDelete($user); -``` - -To mass hard delete records that were soft deleted before a given date, you can use hardDeleteAll($date): - -``` -// in src/Model/Table/UsersTable.php -$date = new \DateTime('some date'); -$affectedRowsCount = $this->hardDeleteAll($date); -``` - -## Soft deleting & associations - -Associations are correctly handled by SoftDelete plugin. - -1. Soft deletion will be cascaded to related models as usual. If related models also use SoftDelete Trait, they will be soft deleted. -2. Soft deletes records will be excluded from counter caches. diff --git a/plugins/cakephp-soft-delete/composer.json b/plugins/cakephp-soft-delete/composer.json deleted file mode 100644 index 1c372022ca..0000000000 --- a/plugins/cakephp-soft-delete/composer.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "name": "imo-tikuwa/cakephp-soft-delete", - "description": "SoftDelete plugin for CakePHP", - "keywords": ["cakephp", "cakephp 4", "plugin", "soft", "delete", "deletable"], - "homepage": "https://github.com/imo-tikuwa/cakephp-soft-delete", - "type": "cakephp-plugin", - "license": "MIT", - "support": { - "source": "https://github.com/imo-tikuwa/cakephp-soft-delete" - }, - "require": { - "php": ">=8.1", - "cakephp/plugin-installer": "^2.0", - "cakephp/cakephp": "5.0.*" - }, - "require-dev": { - "cakephp/cakephp-codesniffer": "^5.0", - "phpunit/phpunit": "^10.1.0", - "cakephp/migrations": "^4.0.0" - }, - "autoload": { - "psr-4": { - "SoftDelete\\": "src" - } - }, - "autoload-dev": { - "psr-4": { - "SoftDelete\\Test\\": "tests", - "Cake\\Test\\": "../../vendor/cakephp/cakephp/tests" - } - }, - "scripts": { - "check": [ - "@test", - "@cs-check" - ], - "cs-check": "phpcs --colors -p src/ tests/", - "test": "phpunit --colors=always" - } -} diff --git a/plugins/cakephp-soft-delete/config/schema/schema.sql b/plugins/cakephp-soft-delete/config/schema/schema.sql deleted file mode 100644 index 3d36ffbc21..0000000000 --- a/plugins/cakephp-soft-delete/config/schema/schema.sql +++ /dev/null @@ -1,28 +0,0 @@ --- sqlite schema. -CREATE TABLE `users` ( - `id` INTEGER PRIMARY KEY AUTOINCREMENT, - `posts_count` integer NOT NULL DEFAULT 0, - `deleted` datetime DEFAULT NULL -); - -CREATE TABLE `tags` ( - `id` INTEGER PRIMARY KEY AUTOINCREMENT, - `name` text DEFAULT NULL, - `deleted_date` datetime DEFAULT NULL -); - -CREATE TABLE `posts` ( - `id` INTEGER PRIMARY KEY AUTOINCREMENT, - `user_id` integer NOT NULL DEFAULT 0, - `deleted` datetime DEFAULT NULL -); -CREATE INDEX `posts_user_id_idx` ON `posts`(`user_id`); - -CREATE TABLE `posts_tags` ( - `id` INTEGER PRIMARY KEY AUTOINCREMENT, - `post_id` integer DEFAULT NULL, - `tag_id` integer DEFAULT NULL, - `deleted` datetime DEFAULT NULL -); -CREATE INDEX `posts_tags_post_id_idx` ON `posts_tags`(`post_id`); -CREATE INDEX `posts_tags_tag_id_idx` ON `posts_tags`(`tag_id`); \ No newline at end of file diff --git a/plugins/cakephp-soft-delete/phpcs.xml b/plugins/cakephp-soft-delete/phpcs.xml deleted file mode 100644 index dcacbcfd05..0000000000 --- a/plugins/cakephp-soft-delete/phpcs.xml +++ /dev/null @@ -1,155 +0,0 @@ - - - CakePHP coding standard - - \.git - - - - - - - - - - 0 - - - 0 - - - 0 - - - 0 - - - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - - - - - - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */config/* - */tests/* - - - */tests/* - - - */tests/* - - - */CakePHP/* - */tests/* - - - */tests/* - - - */tests/* - - - */src/* - */tests/* - - - */src/* - */tests/* - - - */src/* - */tests/* - - - - diff --git a/plugins/cakephp-soft-delete/phpunit.xml.dist b/plugins/cakephp-soft-delete/phpunit.xml.dist deleted file mode 100644 index cbee9f0f8b..0000000000 --- a/plugins/cakephp-soft-delete/phpunit.xml.dist +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - - - ./tests/TestCase - - - - - - - - - - - - ./vendor/ - ./vendor/ - - ./tests/ - ./tests/ - - - - diff --git a/plugins/cakephp-soft-delete/src/Error/MissingColumnException.php b/plugins/cakephp-soft-delete/src/Error/MissingColumnException.php deleted file mode 100644 index 4f23a00605..0000000000 --- a/plugins/cakephp-soft-delete/src/Error/MissingColumnException.php +++ /dev/null @@ -1,9 +0,0 @@ -belongsTo('Users'); - $this->belongsToMany('Tags'); - $this->addBehavior('CounterCache', ['Users' => ['posts_count']]); - } -} - - -class PostsFixture extends TestFixture -{ - public $records = [ - [ - 'id' => 1, - 'user_id' => 1, - 'deleted' => null, - ], - [ - 'id' => 2, - 'user_id' => 1, - 'deleted' => null, - ], - ]; -} diff --git a/plugins/cakephp-soft-delete/tests/Fixture/PostsTagsFixture.php b/plugins/cakephp-soft-delete/tests/Fixture/PostsTagsFixture.php deleted file mode 100644 index 2bcc55a8f7..0000000000 --- a/plugins/cakephp-soft-delete/tests/Fixture/PostsTagsFixture.php +++ /dev/null @@ -1,42 +0,0 @@ -belongsTo('Tags'); - $this->belongsTo('Posts'); - } -} - - -class PostsTagsFixture extends TestFixture -{ - public $records = [ - [ - 'id' => 1, - 'post_id' => 1, - 'tag_id' => 1, - 'deleted' => null, - ], - [ - 'id' => 2, - 'post_id' => 1, - 'tag_id' => 2, - 'deleted' => '2015-05-18 15:04:00', - ], - ]; -} diff --git a/plugins/cakephp-soft-delete/tests/Fixture/TagsFixture.php b/plugins/cakephp-soft-delete/tests/Fixture/TagsFixture.php deleted file mode 100644 index e4a91eb55e..0000000000 --- a/plugins/cakephp-soft-delete/tests/Fixture/TagsFixture.php +++ /dev/null @@ -1,52 +0,0 @@ -belongsToMany('Posts', [ - 'through' => 'PostsTags', - 'joinTable' => 'posts_tags', - 'foreignKey' => 'tag_id', - 'targetForeignKey' => 'post_id' - ]); - $this->hasMany('PostsTags'); - } -} - - -class TagsFixture extends TestFixture -{ - public $records = [ - [ - 'id' => 1, - 'name' => 'Cat', - 'deleted_date' => null, - ], - [ - 'id' => 2, - 'name' => 'Dog', - 'deleted_date' => null, - ], - [ - 'id' => 3, - 'name' => 'Fish', - 'deleted_date' => '2015-04-15 09:46:00', - ] - ]; -} diff --git a/plugins/cakephp-soft-delete/tests/Fixture/UsersFixture.php b/plugins/cakephp-soft-delete/tests/Fixture/UsersFixture.php deleted file mode 100644 index 7392a4099e..0000000000 --- a/plugins/cakephp-soft-delete/tests/Fixture/UsersFixture.php +++ /dev/null @@ -1,41 +0,0 @@ -hasMany('Posts', [ - 'dependent' => true, - 'cascadeCallbacks' => true, - ]); - } -} - -class UsersFixture extends TestFixture -{ - public $records = [ - [ - 'id' => 1, - 'deleted' => null, - 'posts_count' => 2 - ], - [ - 'id' => 2, - 'deleted' => null, - 'posts_count' => 0 - ], - ]; -} diff --git a/plugins/cakephp-soft-delete/tests/Table/PostsTable.php b/plugins/cakephp-soft-delete/tests/Table/PostsTable.php deleted file mode 100644 index a9889c9500..0000000000 --- a/plugins/cakephp-soft-delete/tests/Table/PostsTable.php +++ /dev/null @@ -1,47 +0,0 @@ -belongsTo('Users'); - $this->belongsToMany('Tags'); - $this->addBehavior('CounterCache', ['Users' => ['posts_count']]); - } - - /** - * Default validation rules. - * - * @param \Cake\Validation\Validator $validator Validator instance. - * @return \Cake\Validation\Validator - */ - public function validationDefault(Validator $validator): Validator - { - $validator - ->integer('id') - ->allowEmptyString('id', null, 'create'); - $validator - ->integer('user_id') - ->notEmptyString('user_id'); - - return $validator; - } -} diff --git a/plugins/cakephp-soft-delete/tests/Table/PostsTagsTable.php b/plugins/cakephp-soft-delete/tests/Table/PostsTagsTable.php deleted file mode 100644 index 25d23a86ff..0000000000 --- a/plugins/cakephp-soft-delete/tests/Table/PostsTagsTable.php +++ /dev/null @@ -1,49 +0,0 @@ -belongsTo('Tags'); - $this->belongsTo('Posts'); - } - - /** - * Default validation rules. - * - * @param \Cake\Validation\Validator $validator Validator instance. - * @return \Cake\Validation\Validator - */ - public function validationDefault(Validator $validator): Validator - { - $validator - ->integer('id') - ->allowEmptyString('id', null, 'create'); - $validator - ->integer('post_id') - ->notEmptyString('post_id'); - $validator - ->integer('tag_id') - ->notEmptyString('tag_id'); - - return $validator; - } -} diff --git a/plugins/cakephp-soft-delete/tests/Table/TagsTable.php b/plugins/cakephp-soft-delete/tests/Table/TagsTable.php deleted file mode 100644 index 4746e5508b..0000000000 --- a/plugins/cakephp-soft-delete/tests/Table/TagsTable.php +++ /dev/null @@ -1,56 +0,0 @@ -belongsToMany('Posts', [ - 'through' => 'PostsTags', - 'joinTable' => 'posts_tags', - 'foreignKey' => 'tag_id', - 'targetForeignKey' => 'post_id' - ]); - $this->hasMany('PostsTags'); - } - - /** - * Default validation rules. - * - * @param \Cake\Validation\Validator $validator Validator instance. - * @return \Cake\Validation\Validator - */ - public function validationDefault(Validator $validator): Validator - { - $validator - ->integer('id') - ->allowEmptyString('id', null, 'create'); - $validator - ->requirePresence('name') - ->allowEmptyString('name'); - - return $validator; - } -} diff --git a/plugins/cakephp-soft-delete/tests/Table/UsersTable.php b/plugins/cakephp-soft-delete/tests/Table/UsersTable.php deleted file mode 100644 index d0798bfc44..0000000000 --- a/plugins/cakephp-soft-delete/tests/Table/UsersTable.php +++ /dev/null @@ -1,48 +0,0 @@ -hasMany('Posts', [ - 'dependent' => true, - 'cascadeCallbacks' => true, - ]); - } - - /** - * Default validation rules. - * - * @param \Cake\Validation\Validator $validator Validator instance. - * @return \Cake\Validation\Validator - */ - public function validationDefault(Validator $validator): Validator - { - $validator - ->integer('id') - ->allowEmptyString('id', null, 'create'); - $validator - ->integer('posts_count') - ->notEmptyString('posts_count'); - - return $validator; - } -} diff --git a/plugins/cakephp-soft-delete/tests/TestCase/Model/Table/SoftDeleteTraitTest.php b/plugins/cakephp-soft-delete/tests/TestCase/Model/Table/SoftDeleteTraitTest.php deleted file mode 100644 index 55b1dbbec9..0000000000 --- a/plugins/cakephp-soft-delete/tests/TestCase/Model/Table/SoftDeleteTraitTest.php +++ /dev/null @@ -1,307 +0,0 @@ -usersTable = $this->getTableLocator()->get('Users', ['className' => \SoftDelete\Test\Table\UsersTable::class]); - $this->postsTable = $this->getTableLocator()->get('Posts', ['className' => \SoftDelete\Test\Table\PostsTable::class]); - $this->tagsTable = $this->getTableLocator()->get('Tags', ['className' => \SoftDelete\Test\Table\TagsTable::class]); - $this->postsTagsTable = $this->getTableLocator()->get('PostsTags', ['className' => \SoftDelete\Test\Table\PostsTagsTable::class]); - } - - /** - * tearDown method - * - * @return void - */ - public function tearDown(): void - { - unset($this->usersTable); - unset($this->postsTable); - unset($this->tagsTable); - parent::tearDown(); - } - - /** - * Tests that a soft deleted entities is not found when calling Table::find() - */ - public function testFind() - { - $user = $this->usersTable->get(1); - $user->deleted = date('Y-m-d H:i:s'); - $this->usersTable->save($user); - - $user = $this->usersTable->find()->where(['id' => 1])->first(); - $this->assertEquals(null, $user); - } - - /** - * Tests that a soft deleted entities is not found when calling Table::findByXXX() - */ - public function testDynamicFinder() - { - $user = $this->usersTable->get(1); - $user->deleted = date('Y-m-d H:i:s'); - $this->usersTable->save($user); - - $user = $this->usersTable->findById(1)->first(); - $this->assertEquals(null, $user); - } - - public function testFindWithOrWhere() - { - $conditions = [ - 'OR' => [ - ['id' => 1], - ['id' => 2] - ] - ]; - $query = $this->usersTable->find()->where($conditions); - $this->assertEquals(2, $query->count()); - - $user = $this->usersTable->get(2); - $this->usersTable->delete($user); - - $query = $this->usersTable->find()->where($conditions); - $this->assertEquals(1, $query->count()); - } - - /** - * Tests that soft deleted records in join table are taken into account when retrieving - * an entity with a belongsToManyAssociation. - */ - public function testFindBelongsToMany() - { - $post = $this->postsTable->findById(1)->contain('Tags')->first(); - $this->assertEquals(1, count($post->tags)); - } - - /** - * Test that entities matching a soft deleted associated record are not returned when using $query->matching(). - */ - public function testFindMatching() - { - $users = $this->usersTable->find() - ->matching('Posts', function ($q) { - return $q->where(['Posts.id' => 1]); - }); - $this->assertEquals(1, $users->count()); - - $post = $this->postsTable->get(1); - $this->postsTable->delete($post); - - $posts = $this->postsTable->find('all', ...['withDeleted'])->where(['id' => 1]); - $this->assertEquals(1, $posts->count()); - - $users = $this->usersTable->find() - ->matching('Posts', function ($q) { - return $q->where(['Posts.id' => 1]); - }); - $this->assertEquals(0, $users->count()); - } - - - /** - * Tests that Table::deleteAll() does not hard delete - */ - public function testDeleteAll() - { - $this->usersTable->deleteAll([]); - $this->assertEquals(0, $this->usersTable->find()->count()); - $this->assertNotEquals(0, $this->usersTable->find('all', ...['withDeleted'])->count()); - - $this->postsTable->deleteAll([]); - $this->assertEquals(0, $this->postsTable->find()->count()); - $this->assertNotEquals(0, $this->postsTable->find('all', ...['withDeleted'])->count()); - } - - /** - * Tests that Table::delete() does not hard delete. - */ - public function testDelete() - { - $user = $this->usersTable->get(1); - $this->usersTable->delete($user); - $user = $this->usersTable->findById(1)->first(); - $this->assertEquals(null, $user); - - $user = $this->usersTable->find('all', ...['withDeleted'])->where(['id' => 1])->first(); - $this->assertNotEquals(null, $user); - $this->assertNotEquals(null, $user->deleted); - } - - /** - * Tests that soft deleting an entity also soft deletes its belonging entities. - */ - public function testHasManyAssociation() - { - $user = $this->usersTable->get(1); - $this->usersTable->delete($user); - - $count = $this->postsTable->find()->where(['user_id' => 1])->count(); - $this->assertEquals(0, $count); - - $count = $this->postsTable->find('all', ...['withDeleted'])->where(['user_id' => 1])->count(); - $this->assertEquals(2, $count); - } - - /** - * Tests that soft deleting affects counters the same way that hard deleting. - */ - public function testCounterCache() - { - $post = $this->postsTable->get(1); - $this->postsTable->delete($post); - $this->assertNotEquals(null, $this->postsTable->find('all', ...['withDeleted'])->where(['id' => 1])->first()); - $this->assertEquals(null, $this->postsTable->findById(1)->first()); - - $user = $this->usersTable->get(1); - $this->assertEquals(1, $user->posts_count); - } - - public function testHardDelete() - { - $user = $this->usersTable->get(1); - $this->usersTable->hardDelete($user); - $user = $this->usersTable->findById(1)->first(); - $this->assertEquals(null, $user); - - $user = $this->usersTable->find('all', ...['withDeleted'])->where(['id' => 1])->first(); - $this->assertEquals(null, $user); - } - - /** - * Tests hardDeleteAll. - */ - public function testHardDeleteAll() - { - $affectedRows = $this->postsTable->hardDeleteAll(new \DateTime('now')); - $this->assertEquals(0, $affectedRows); - - $postsRowsCount = $this->postsTable->find('all', ...['withDeleted'])->count(); - - $this->postsTable->delete($this->postsTable->get(1)); - $affectedRows = $this->postsTable->hardDeleteAll(new \DateTime('now')); - $this->assertEquals(1, $affectedRows); - - $newpostsRowsCount = $this->postsTable->find('all', ...['withDeleted'])->count(); - $this->assertEquals($postsRowsCount - 1, $newpostsRowsCount); - } - - /** - * Using a table with a custom soft delete field, ensure we can still filter - * the found results properly. - * - * @return void - */ - public function testFindingWithCustomField() - { - $query = $this->tagsTable->find(); - $this->assertEquals(2, $query->count()); - - $query = $this->tagsTable->find('all', ...['withDeleted' => true]); - $this->assertEquals(3, $query->count()); - } - - /** - * Ensure that when deleting a record which has a custom field defined in - * the table, that it is still soft deleted. - * - * @return void - */ - public function testDeleteWithCustomField() - { - $tag = $this->tagsTable->get(1); - $this->tagsTable->delete($tag); - - $query = $this->tagsTable->find(); - $this->assertEquals(1, $query->count()); - } - - /** - * With a custom soft delete field ensure that a soft deleted record can - * still be permanently removed. - * - * @return void - */ - public function testHardDeleteWithCustomField() - { - $tag = $this->tagsTable->find('all', ...['withDeleted']) - ->where(['id' => 2]) - ->first(); - - $this->tagsTable->hardDelete($tag); - - $tag = $this->tagsTable->find('all', ...['withDeleted']) - ->where(['id' => 2]) - ->first(); - - $this->assertEquals(null, $tag); - } - - /** - * Test soft deleting and restoring a record. - * @return void - */ - public function testRestore() - { - $user = $this->usersTable->findById(1)->first(); - $this->assertNotNull($user); - $this->usersTable->delete($user); - $user = $this->usersTable->findById(1)->first(); - $this->assertNull($user); - - $user = $this->usersTable->find('all', ...['withDeleted'])->where(['id' => 1])->first(); - $this->usersTable->restore($user); - $user = $this->usersTable->findById(1)->first(); - $this->assertNotNull($user); - } - - /** - * When a configured field is missing from the table, an exception should be thrown - * - */ - public function testMissingColumn() - { - $this->expectException(\SoftDelete\Error\MissingColumnException::class); - $this->postsTable->softDeleteField = 'foo'; - $post = $this->postsTable->get(1); - $this->postsTable->delete($post); - } -} diff --git a/plugins/cakephp-soft-delete/tests/bootstrap.php b/plugins/cakephp-soft-delete/tests/bootstrap.php deleted file mode 100644 index 4b392f0325..0000000000 --- a/plugins/cakephp-soft-delete/tests/bootstrap.php +++ /dev/null @@ -1,44 +0,0 @@ -run(); -(new \Cake\TestSuite\Fixture\SchemaLoader())->loadSqlFiles($schema_path, 'test'); - -if (file_exists($root . '/config/bootstrap.php')) { - require $root . '/config/bootstrap.php'; - - return; -}