From 0628827be4869960dfe5960823512005a825fa52 Mon Sep 17 00:00:00 2001 From: JJ Date: Wed, 17 Jul 2024 09:58:01 -0400 Subject: [PATCH] add test for WordPress SEO connector currently fails to reproduce https://github.com/xwp/stream/issues/1489, https://github.com/xwp/stream/issues/1443 --- .gitignore | 1 + composer.json | 5 ++ composer.lock | 20 ++++- local/public/wp-config.php | 1 + local/public/wp-content/plugins/hello.php | 1 + package.json | 1 + phpunit.xml | 2 +- tests/bootstrap.php | 1 + .../test-class-connector-wordpress-seo.php | 83 +++++++++++++++++++ 9 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 tests/tests/connectors/test-class-connector-wordpress-seo.php diff --git a/.gitignore b/.gitignore index 651787857..b472ff1b6 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ /stream.zip /stream-*.zip npm-debug.log +debug.log package.lock .phpunit.result.cache diff --git a/composer.json b/composer.json index cd06d1236..91e3ba8f5 100644 --- a/composer.json +++ b/composer.json @@ -31,6 +31,7 @@ "wpackagist-plugin/easy-digital-downloads": "2.9.23", "wpackagist-plugin/jetpack": "10.0", "wpackagist-plugin/user-switching": "1.5.5", + "wpackagist-plugin/wordpress-seo": "23.0", "wpackagist-theme/twentytwentythree": "^1.0", "xwp/wait-for": "^0.0.1", "yoast/phpunit-polyfills": "^1.1" @@ -70,6 +71,10 @@ "phpunit --coverage-text", "php local/scripts/make-clover-relative.php ./tests/reports/clover.xml" ], + "test-one": [ + "phpunit", + "WP_MULTISITE=1 phpunit" + ], "test-multisite": [ "WP_MULTISITE=1 phpunit --coverage-text", "php local/scripts/make-clover-relative.php ./tests/reports/clover.xml" diff --git a/composer.lock b/composer.lock index a08bca72c..7ffeacaa2 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "abf1540362c6e039be43d3cb8d21bc8d", + "content-hash": "ebfa2a164fed755007fb4579a422a26e", "packages": [ { "name": "composer/installers", @@ -8408,6 +8408,24 @@ "type": "wordpress-plugin", "homepage": "https://wordpress.org/plugins/user-switching/" }, + { + "name": "wpackagist-plugin/wordpress-seo", + "version": "23.0", + "source": { + "type": "svn", + "url": "https://plugins.svn.wordpress.org/wordpress-seo/", + "reference": "tags/23.0" + }, + "dist": { + "type": "zip", + "url": "https://downloads.wordpress.org/plugin/wordpress-seo.23.0.zip" + }, + "require": { + "composer/installers": "^1.0 || ^2.0" + }, + "type": "wordpress-plugin", + "homepage": "https://wordpress.org/plugins/wordpress-seo/" + }, { "name": "wpackagist-theme/twentytwentythree", "version": "1.4", diff --git a/local/public/wp-config.php b/local/public/wp-config.php index 42734f680..ae947c5ec 100644 --- a/local/public/wp-config.php +++ b/local/public/wp-config.php @@ -25,6 +25,7 @@ $table_prefix = 'wp_'; define( 'WP_DEBUG', true ); +define( 'WP_DEBUG_LOG', true ); define( 'JETPACK_DEV_DEBUG', true ); // Keep the wp-contents outside of WP core directory. diff --git a/local/public/wp-content/plugins/hello.php b/local/public/wp-content/plugins/hello.php index ae51ede30..cbbf79f15 100644 --- a/local/public/wp-content/plugins/hello.php +++ b/local/public/wp-content/plugins/hello.php @@ -4,3 +4,4 @@ * Description: A plugin used for PHP unit tests */ + diff --git a/package.json b/package.json index 8078e420e..114536e4d 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "test": "npm-run-all test:*", "test:php": "npm run cli -- composer test --working-dir=wp-content/plugins/stream-src", "test:php-multisite": "npm run cli -- composer test-multisite --working-dir=wp-content/plugins/stream-src", + "test:php:one": "npm run cli -- composer test-one --working-dir=wp-content/plugins/stream-src --", "test-report": "npm run cli -- composer test-report --working-dir=wp-content/plugins/stream-src", "build-containers": "docker compose --file docker-compose.build.yml build", "push-containers": "docker compose --file docker-compose.build.yml push", diff --git a/phpunit.xml b/phpunit.xml index f37a7bf54..1254c9721 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -10,7 +10,7 @@ diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 06b12bd93..8e445990b 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -93,6 +93,7 @@ function( $status = false, $args = array(), $url = '') { define( 'EDD_USE_PHP_SESSIONS', false ); define( 'WP_USE_THEMES', false ); activate_plugin( 'easy-digital-downloads/easy-digital-downloads.php' ); +activate_plugin( 'wordpress-seo/wp-seo.php' ); xwp_install_edd(); require __DIR__ . '/testcase.php'; diff --git a/tests/tests/connectors/test-class-connector-wordpress-seo.php b/tests/tests/connectors/test-class-connector-wordpress-seo.php new file mode 100644 index 000000000..b9a5fa271 --- /dev/null +++ b/tests/tests/connectors/test-class-connector-wordpress-seo.php @@ -0,0 +1,83 @@ +plugin->connectors->unload_connectors(); + + // Make partial of Connector_WordPress_SEO class, with mocked "log" function. + $this->mock = $this->getMockBuilder( Connector_WordPress_SEO::class ) + ->setMethods( array( 'log' ) ) + ->getMock(); + + // Register connector. + $this->mock->register(); + } + + /** + * Confirm that WordPress SEO is installed and active. + */ + public function test_wordpress_seo_installed_and_activated() { + $this->assertTrue( defined( 'YOAST_ENVIRONMENT' ) ); + } + + /** + * Tests "added_post_meta" callback function. + * callback_added_post_meta( $meta_id, $object_id, $meta_key, $meta_value ) + */ + public function test_callback_added_post_meta() { + + // Set expected calls for the Mock. + $this->mock->expects( $this->once() ) + ->method( 'log' ) + ->with( + $this->equalTo( + __( 'Updated "SEO title" of "Test post %%!" Post', 'stream' ) + ), + $this->equalTo( + array( + 'meta_key' => $this->title_meta_key, + 'meta_value' => 'Test meta %!', + 'post_type' => 'post', + ) + ), + $this->greaterThan( 0 ), + 'wpseo_meta', + 'updated' + ); + + // Create post for later use. + $post_id = wp_insert_post( + array( + 'post_title' => 'Test post %!', + 'post_content' => 'Lorem ipsum dolor...', + 'post_status' => 'publish' + ) + ); + + update_post_meta( $post_id, $this->title_meta_key, 'Test meta %!' ); + + // Confirm callback execution. + $this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_added_post_meta' ) ); + } +}