Skip to content

Commit

Permalink
Correct usage of UPLOADS with tests (#73)
Browse files Browse the repository at this point in the history
* Correct usage of UPLOADS with tests
  • Loading branch information
kraftbj authored Feb 12, 2025
1 parent a322bbe commit 33e7d5a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 31 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
],
"require": {
"php": ">=7.2.24",
"roots/wordpress": "^6.6.2"
"roots/wordpress": "^6.6"
},
"require-dev": {
"phpunit/phpunit": "^7.5 || ^9.5",
Expand Down
50 changes: 25 additions & 25 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function tear_down_wordbless() {
* @return void
*/
public function clear_uploads() {
$uploads_folder = ABSPATH . '/wp-content/uploads';
$uploads_folder = wp_get_upload_dir()['basedir'];
$scan = glob( rtrim( $uploads_folder, '/' ) . '/*' );

foreach ( $scan as $path ) {
Expand Down
4 changes: 2 additions & 2 deletions src/Load.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public static function load() {
require ABSPATH . '/wp-settings.php';
require_once ABSPATH . 'wp-admin/includes/admin.php';
// UPLOADS is defined by the time we get here, via a bootstrap.php or the code block above.
if ( ! file_exists( UPLOADS ) ) { // @phpstan-ignore constant.notFound
mkdir( UPLOADS ); // @phpstan-ignore constant.notFound
if ( ! file_exists( ABSPATH . UPLOADS ) ) { // @phpstan-ignore constant.notFound
mkdir( ABSPATH . UPLOADS ); // @phpstan-ignore constant.notFound
}

Options::init();
Expand Down
10 changes: 9 additions & 1 deletion tests/test-setup-teardown.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@ public function test_setup_called() {
* @covers Load::load
*/
public function test_default_uploads_directory() {
$this->assertEquals(WP_CONTENT_DIR . '/uploads', \UPLOADS);
$this->assertEquals(WP_CONTENT_DIR . '/uploads', ABSPATH . UPLOADS);
}

/**
* This verifies that WordPress sees the default uploads directory.
* @covers Load::load
*/
public function test_default_uploads_directory_is_recognized() {
$this->assertEquals( wp_upload_dir()['path'], ABSPATH . UPLOADS);
}

/**
* @depends test_setup_called
*/
Expand Down
23 changes: 22 additions & 1 deletion tests/test-uploads.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ class Test_Uploads extends BaseTestCase {
* The default directory is tested in test-setup-teardown.php.
*/
public function test_custom_uploads_directory() {
$this->assertEquals(WP_CONTENT_DIR . '/custom-uploads', UPLOADS);
$this->assertEquals(WP_CONTENT_DIR . '/custom-uploads', ABSPATH . UPLOADS);
}

/**
* @covers Load::load
* @covers dbless_UPLOADS
*
* This tests the custom uploads directory is created correctly by Load::load.
*
* This test must run before any tests that use the uploads directory or call wp_upload_dir() which will create the uploads directory if it doesn't exist.
*/
public function test_custom_uploads_directory_is_created() {
$this->assertTrue(file_exists(ABSPATH . UPLOADS));
}

/**
* @covers Load::load
*
* This test confirms that WordPress sees the custom uploads directory.
*/
public function test_custom_uploads_directory_is_recognized() {
$this->assertEquals( wp_upload_dir()['path'], ABSPATH . UPLOADS);
}
}

0 comments on commit 33e7d5a

Please sign in to comment.