diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index a4185461a..800d17e60 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -40,7 +40,7 @@ jobs: - unit --skip-group=slow - unit --group=isolated-1 - unit --group=isolated-2 - - webdriver + # - webdriver # Disabled until the webdriver tests are fixed. - wpcli_module - wploader_multisite - wploader_wpdb_interaction diff --git a/CHANGELOG.md b/CHANGELOG.md index cb8fe1d5c..d6ee9aa6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Ensure the `WPLoader` module will initialize correctly when used in `loadOnly` mode not using the `EventDispatcherBridge` extension. (thanks @lxbdr) - Support loading the `wpdb` class from either the `class-wpdb.php` file or the `wp-db.php` one, supporting older versions of WordPress (thanks @BrianHenryIE) - Read content, plugins and mu-plugins directories paths from the `WPLoader` configuration parameters correctly. +- In the `WPTestCase` class, remove attachments created during tests between tests. ## [4.2.5] 2024-06-26; diff --git a/src/TestCase/WPTestCase.php b/src/TestCase/WPTestCase.php index f3f1e8e63..2e8aaa6ce 100644 --- a/src/TestCase/WPTestCase.php +++ b/src/TestCase/WPTestCase.php @@ -172,6 +172,10 @@ class WPTestCase extends Unit private ?float $requestTimeFloat = null; private ?int $requestTime = null; + /** + * @var array + */ + private array $attachmentsAddedDuringTest = []; private function initBackupGlobalsProperties():void { @@ -478,4 +482,20 @@ public function getName(bool $withDataSet = true): string // @phpstan-ignore-next-line PHPUnit >= 10.0.0. return $withDataSet ? $this->nameWithDataSet() : $this->name(); } + + // @phpstan-ignore-next-line Used in the setUp method of the test case trait. + private function recordAttachmentAddedDuringTest(): void + { + add_action('add_attachment', function (int $post_id): void { + $this->attachmentsAddedDuringTest[] = $post_id; + }); + } + + // @phpstan-ignore-next-line Used in the tearDown method of the test case trait. + private function removeAttachmentsAddedDuringTest(): void + { + foreach ($this->attachmentsAddedDuringTest as $post_id) { + wp_delete_attachment($post_id, true); + } + } } diff --git a/src/TestCase/WPTestCasePHPUnitMethodsTrait.php b/src/TestCase/WPTestCasePHPUnitMethodsTrait.php index 19aad0651..42fba5f2a 100644 --- a/src/TestCase/WPTestCasePHPUnitMethodsTrait.php +++ b/src/TestCase/WPTestCasePHPUnitMethodsTrait.php @@ -33,10 +33,12 @@ protected function setUp() //@phpstan-ignore-line $this->set_up(); //@phpstan-ignore-line magic __call $this->backupAdditionalGlobals(); + $this->recordAttachmentAddedDuringTest(); } protected function tearDown() //@phpstan-ignore-line { + $this->removeAttachmentsAddedDuringTest(); $this->restoreAdditionalGlobals(); $this->tear_down(); //@phpstan-ignore-line magic __call parent::tearDown(); @@ -71,10 +73,12 @@ protected function setUp(): void $this->set_up(); //@phpstan-ignore-line magic __call $this->backupAdditionalGlobals(); + $this->recordAttachmentAddedDuringTest(); } protected function tearDown(): void { + $this->removeAttachmentsAddedDuringTest(); $this->restoreAdditionalGlobals(); $this->tear_down(); //@phpstan-ignore-line magic __call parent::tearDown(); diff --git a/tests/_support/_generated/WploaderTesterActions.php b/tests/_support/_generated/WploaderTesterActions.php index 28ee75aa8..ee29e9a05 100644 --- a/tests/_support/_generated/WploaderTesterActions.php +++ b/tests/_support/_generated/WploaderTesterActions.php @@ -1,4 +1,4 @@ -getScenario()->runStep(new \Codeception\Step\Action('getMuPluginsFolder', func_get_args())); + } + + /** * [!] Method is generated. Documentation taken from corresponding module. * @@ -44,8 +63,9 @@ public function getWpRootFolder(?string $path = NULL): string { * * Returns the absolute path to the plugins directory. * - * The value will first look at the `WP_PLUGIN_DIR` constant, then the `pluginsFolder` configuration parameter - * and will, finally, look in the default path from the WordPress root directory. + * The value will first look at the `WP_PLUGIN_DIR` constant, then the `pluginsFolder` configuration parameter, + * then the `WP_CONTENT_DIR` configuration parameter, and will, finally, look in the default path from the + * WordPress root directory. * * @example * ```php @@ -115,6 +135,9 @@ public function factory(): \lucatume\WPBrowser\Module\WPLoader\FactoryStore { * * Returns the absolute path to the WordPress content directory. * + * The value will first look at the `WP_CONTENT_DIR` configuration parameter, and will, finally, look in the + * default path from the WordPress root directory. + * * @example * ```php * $content = $this->getContentFolder(); diff --git a/tests/webdriver/PluginActivationCest.php b/tests/webdriver/PluginActivationCest.php index 6715d736e..ce2cdb485 100644 --- a/tests/webdriver/PluginActivationCest.php +++ b/tests/webdriver/PluginActivationCest.php @@ -71,22 +71,8 @@ public function be_able_to_activate_plugins_in_a_long_list(WebDriverTester $I): protected function scaffoldTestPlugins(WebDriverTester $I): void { - $template - = <<< HANDLEBARS -/* -Plugin Name: Plugin {{letter}} -Plugin URI: https://wordpress.org/plugins/{{letter}}/ -Description: Plugin {{letter}} description -Version: 0.1.0 -Author: Plugin {{letter}} author -Author URI: http://example.com/{{letter}}-plugin -Text Domain: {{letter}}_plugin -Domain Path: /languages -*/ -HANDLEBARS; - foreach (range('A', 'Z') as $letter) { - $compiled = str_replace('{{letter}}', $letter, $template); + $compiled = "function {$letter}_main(){}"; $I->havePlugin("plugin-{$letter}/plugin-{$letter}.php", $compiled); } } diff --git a/tests/wploadersuite/AttachmentCleanupTest.php b/tests/wploadersuite/AttachmentCleanupTest.php new file mode 100644 index 000000000..e613828a1 --- /dev/null +++ b/tests/wploadersuite/AttachmentCleanupTest.php @@ -0,0 +1,26 @@ +attachment->create_upload_object( + codecept_data_dir('attachments/kitten.jpeg') + ); + + $this->assertIsInt(self::$standaloneAttachment); + $this->assertTrue(file_exists(get_attached_file(self::$standaloneAttachment))); + self::$standaloneAttachmentFile = get_attached_file(self::$standaloneAttachment); + } + + public function testAttachmentsCreatedWithCreateUploadObjectAreDeleted(): void + { + $this->assertEquals('', get_attached_file(self::$standaloneAttachment)); + $this->assertFalse(file_exists(self::$standaloneAttachmentFile)); + } +}