diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh index 413a583..cba6728 100755 --- a/bin/install-wp-tests.sh +++ b/bin/install-wp-tests.sh @@ -148,8 +148,8 @@ install_db() { EXTRA=" --host=$DB_HOSTNAME --protocol=tcp" fi fi - - local exists=$(mysql -s -N -e "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME='$DB_NAME'"); + + local exists=$(mysql -u $DB_USER -p"$DB_PASS" -s -N -e "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME='$DB_NAME'"); if [ -n "$exists" ]; then yes | mysqladmin drop $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA fi diff --git a/composer.json b/composer.json index f14ee28..c5b9946 100644 --- a/composer.json +++ b/composer.json @@ -50,5 +50,11 @@ "tests/", "vendor_namespaced/" ] + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true, + "composer/installers": true + } } } diff --git a/docs/TESTS.md b/docs/TESTS.md index c732939..5aa918a 100644 --- a/docs/TESTS.md +++ b/docs/TESTS.md @@ -9,6 +9,10 @@ Please make sure you check the version of the PHPUnit Documentation you're viewi ### Setup +#### Dependencies +- svn +- mysql (and mysqladmin) + #### Files and Database First, set up your tests database by running the following command in the root plugin directory. Make sure you substitute your local root mysql password. @@ -18,7 +22,7 @@ cd wp-discourse bash bin/install-wp-tests.sh wordpress_test root 'password' localhost latest ``` -If this command returns an error, clean up anything it created before running it again, i.e. +If this command returns an error, clean up anything it created before running it again, i.e. ``` ## Remove the tmp files @@ -36,11 +40,11 @@ Make sure that command completes successfully, with no errors, before continuing #### Environment and Dependencies -If you haven't already, run ``composer install`` in the root plugin directory to pull in the main tests dependencies. +If you haven't already, run ``composer install`` in the root plugin directory to pull in the main tests dependencies. ##### Xdebug -One additional dependency you need in your environment is the php extension Xdebug. The installation of Xdebug is environment specific, however we would recommend you use the [Xdebug Installation Wizard](https://xdebug.org/wizard) to ensure your installation is correct. +One additional dependency you need in your environment is the php extension Xdebug. The installation of Xdebug is environment specific, however we would recommend you use the [Xdebug Installation Wizard](https://xdebug.org/wizard) to ensure your installation is correct. For testing purposes Xdebug should be run in ``coverage`` mode, which means that you should have a line in your ``php.ini`` that look like this @@ -54,7 +58,7 @@ The tests suite is configured by the ``phpunit.xml`` file. Read more about the e #### Coverage -The tests coverage whitelist in the config file limits the coverage reports to the classes in the plugin that have tests. +The tests coverage whitelist in the config file limits the coverage reports to the classes in the plugin that have tests. ``` @@ -81,10 +85,10 @@ vendor/bin/phpunit tests/phpunit/test-discourse-publish.php To run a specific test in a suite use the ``--filter`` option ``` -vendor/bin/phpunit tests/phpunit/test-discourse-publish.php --filter=test_sync_to_discourse_when_creating_with_embed_error +vendor/bin/phpunit tests/phpunit/test-discourse-publish.php --filter=test_sync_to_discourse_when_creating_with_embed_error ``` -To add a coverage report to the output add ``--coverage-text``, which will send the report to stdout. +To add a coverage report to the output add ``--coverage-text``, which will send the report to stdout. ``` vendor/bin/phpunit --coverage-text diff --git a/lib/discourse-publish.php b/lib/discourse-publish.php index 49a770f..fd4a8c5 100644 --- a/lib/discourse-publish.php +++ b/lib/discourse-publish.php @@ -261,10 +261,6 @@ class DiscoursePublish extends DiscourseBase { if ( ! empty( $featured ) ) { $baked = str_replace( '{featuredimage}', '![image](' . $featured['0'] . ')', $baked ); } - $username = apply_filters( 'wpdc_discourse_username', get_the_author_meta( 'discourse_username', $post->post_author ), $author_id ); - if ( ! $username || strlen( $username ) < 2 ) { - $username = $options['publish-username']; - } // Get publish category of a post. $publish_post_category = get_post_meta( $post_id, 'publish_post_category', true ); @@ -372,6 +368,15 @@ class DiscoursePublish extends DiscourseBase { } } + $username = apply_filters( 'wpdc_discourse_username', get_the_author_meta( 'discourse_username', $post->post_author ), $author_id ); + if ( $username && strlen( $username ) > 1 ) { + $change_response = $this->change_post_owner( $post_id, $username ); + + if ( is_wp_error( $change_response ) ) { + return $change_response; + } + } + // The topic has been created and its associated post's metadata has been updated. return null; } @@ -448,6 +453,31 @@ class DiscoursePublish extends DiscourseBase { return $response; } + /** + * Changes the owner of a Discourse topic associated with a WordPress post. + * + * @param int $post_id The WordPress post_id. + * @param string $username The username of the Discourse user to change ownership to. + * + * @return null|\WP_Error + */ + protected function change_post_owner( $post_id, $username ) { + $discourse_post_id = get_post_meta( $post_id, 'discourse_post_id', true ); + $discourse_topic_id = get_post_meta( $post_id, 'discourse_topic_id', true ); + + $path = "/t/$discourse_topic_id/change-owner"; + $body = array( + 'username' => $username, + 'post_ids' => array( $discourse_post_id ), + ); + $options = array( + 'method' => 'POST', + 'body' => $body, + ); + + return $this->remote_post( $path, $options, 'change_owner', $post_id ); + } + /** * Creates an admin_notice and calls the publish_failure_notification method after a bad response is returned from Discourse. * diff --git a/lib/plugin-utilities.php b/lib/plugin-utilities.php index bfb246d..d682d33 100644 --- a/lib/plugin-utilities.php +++ b/lib/plugin-utilities.php @@ -399,7 +399,7 @@ trait PluginUtilities { } if ( isset( $args['method'] ) ) { - $opts['method'] = $args['method']; // default GET. + $opts['method'] = strtoupper( $args['method'] ); // default GET. } $response = wp_remote_request( $url, $opts ); diff --git a/readme.txt b/readme.txt index e142c4d..dd9bd73 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Tags: discourse, forum, comments, sso Requires at least: 4.7 Tested up to: 5.9 Requires PHP: 5.6.0 -Stable tag: 2.3.8 +Stable tag: 2.3.9 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -123,6 +123,12 @@ To create a coherent top menu, see our tutorial on how to make a [Custom nav hea == Changelog == +#### 2.3.9 02/21/2022 + +- Add proper Discourse Username support +- Ensure discourse_request method is uppercase. +- Update tests config. + #### 2.3.8 02/04/2022 - Add Wordpress 5.9 support. diff --git a/tests/phpunit/helpers/remote_post.php b/tests/phpunit/helpers/remote_post.php index e90165d..de61206 100644 --- a/tests/phpunit/helpers/remote_post.php +++ b/tests/phpunit/helpers/remote_post.php @@ -108,9 +108,13 @@ trait RemotePost { if ( $request['method'] != $args['method'] ) { return new \WP_Error( 'http_request_failed', 'Incorrect method' ); - } else { - return $request['response']; } + + if ( isset( $request['body'] ) && $request['body'] != $args['body'] ) { + return new \WP_Error( 'http_request_failed', 'Incorrect body' ); + } + + return $request['response']; }, 10, 3 @@ -134,4 +138,4 @@ trait RemotePost { $this->mock_remote_post( $first_request, $second_request ); return json_decode( $raw_body ); } -} \ No newline at end of file +} diff --git a/tests/phpunit/test-discourse-publish.php b/tests/phpunit/test-discourse-publish.php index 2705b2f..cbf6d07 100644 --- a/tests/phpunit/test-discourse-publish.php +++ b/tests/phpunit/test-discourse-publish.php @@ -245,7 +245,7 @@ class DiscoursePublishTest extends UnitTest { // Enable direct db pubilcation flags option. self::$plugin_options['direct-db-publication-flags'] = 1; $this->publish->setup_options( self::$plugin_options ); - + // Set up a response body for creating a new post. $body = $this->mock_remote_post_success( 'post_create', 'POST' ); $discourse_post_id = $body->id; @@ -306,6 +306,49 @@ class DiscoursePublishTest extends UnitTest { wp_delete_post( $post_id ); } + /** + * Sync_to_discourse when creating a new post with Discourse Username set. + */ + public function test_sync_to_discourse_discourse_username() { + $discourse_username = 'angus'; + + // Setup the user + $user_id = $this->factory->user->create(); + add_user_meta( $user_id, 'discourse_username', $discourse_username, true ); + + // Set up a response body for creating a new post + $discourse_post = json_decode( $this->response_body_file( "post_create" ) ); + $second_request = array( + 'url' => self::$discourse_url . "/t/{$discourse_post->topic_id}/change-owner", + 'method' => 'POST', + 'body' => json_encode(array( + 'username' => $discourse_username, + 'post_ids' => [ "{$discourse_post->id}" ] + )), + 'response' => $this->build_response( 'success' ), + ); + $body = $this->mock_remote_post_success( 'post_create', 'POST', $second_request ); + + // Add the post. + $post_atts = self::$post_atts; + $post_atts['post_author'] = $user_id; + $post_id = wp_insert_post( $post_atts, false, false ); + + // Run the publication. + $post = get_post( $post_id ); + $response = $this->publish->sync_to_discourse_without_lock( + $post_id, + $post->title, + $post->post_content + ); + + // Ensure the right result. + $this->assertFalse( is_wp_error( $response ) ); + + // Cleanup. + wp_delete_post( $post_id ); + } + /** * Sync_to_discourse when updating a post. */ @@ -635,7 +678,7 @@ class DiscoursePublishTest extends UnitTest { $message = __( 'An invalid response was returned from Discourse', 'wp-discourse' ); return new \WP_Error( 'discourse_publishing_response_error', $message ); } - + /** * Build an error notice returned by discourse-publish when post queued or topic deleted. */ @@ -654,7 +697,7 @@ class DiscoursePublishTest extends UnitTest { 'method' => 'POST', 'headers' => array( 'Api-Key' => '1234', - 'Api-Username' => 'angus', + 'Api-Username' => 'system', ), 'body' => http_build_query( array( @@ -674,4 +717,3 @@ class DiscoursePublishTest extends UnitTest { ); } } - diff --git a/wp-discourse.php b/wp-discourse.php index 281dd24..5f327ab 100644 --- a/wp-discourse.php +++ b/wp-discourse.php @@ -2,7 +2,7 @@ /** * Plugin Name: WP-Discourse * Description: Use Discourse as a community engine for your WordPress blog - * Version: 2.3.8 + * Version: 2.3.9 * Author: Discourse * Text Domain: wp-discourse * Domain Path: /languages @@ -34,7 +34,7 @@ define( 'WPDISCOURSE_PATH', plugin_dir_path( __FILE__ ) ); define( 'WPDISCOURSE_URL', plugins_url( '', __FILE__ ) ); define( 'MIN_WP_VERSION', '4.7' ); define( 'MIN_PHP_VERSION', '5.6.0' ); -define( 'WPDISCOURSE_VERSION', '2.3.8' ); +define( 'WPDISCOURSE_VERSION', '2.3.9' ); require_once WPDISCOURSE_PATH . 'lib/plugin-utilities.php'; require_once WPDISCOURSE_PATH . 'lib/template-functions.php';