Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,11 @@
"tests/",
"vendor_namespaced/"
]
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"composer/installers": true
}
}
}
16 changes: 10 additions & 6 deletions docs/TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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.

```
<whitelist processUncoveredFilesFromWhitelist="true">
Expand All @@ -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
Expand Down
38 changes: 34 additions & 4 deletions lib/discourse-publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,6 @@ protected function sync_to_discourse_work( $post_id, $title, $raw ) {
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 );
Expand Down Expand Up @@ -372,6 +368,15 @@ protected function sync_to_discourse_work( $post_id, $title, $raw ) {
}
}

$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;
}
Expand Down Expand Up @@ -448,6 +453,31 @@ protected function pin_discourse_topic( $post_id, $topic_id, $pin_until ) {
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.
*
Expand Down
2 changes: 1 addition & 1 deletion lib/plugin-utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ protected function discourse_request( $url, $args = array() ) {
}

if ( isset( $args['method'] ) ) {
$opts['method'] = $args['method']; // default GET.
$opts['method'] = strtoupper( $args['method'] ); // default GET.
}

$response = wp_remote_request( $url, $opts );
Expand Down
8 changes: 7 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
10 changes: 7 additions & 3 deletions tests/phpunit/helpers/remote_post.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,13 @@ function( $prempt, $args, $url ) use ( $first_request, $second_request ) {

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
Expand All @@ -134,4 +138,4 @@ protected function mock_remote_post_success( $type, $method = 'GET', $second_req
$this->mock_remote_post( $first_request, $second_request );
return json_decode( $raw_body );
}
}
}
50 changes: 46 additions & 4 deletions tests/phpunit/test-discourse-publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function test_sync_to_discourse_when_creating_with_direct_db_publication_
// 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;
Expand Down Expand Up @@ -306,6 +306,49 @@ public function test_sync_to_discourse_pin_topic() {
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.
*/
Expand Down Expand Up @@ -635,7 +678,7 @@ protected function build_body_error() {
$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.
*/
Expand All @@ -654,7 +697,7 @@ public static function initialize_variables() {
'method' => 'POST',
'headers' => array(
'Api-Key' => '1234',
'Api-Username' => 'angus',
'Api-Username' => 'system',
),
'body' => http_build_query(
array(
Expand All @@ -674,4 +717,3 @@ public static function initialize_variables() {
);
}
}

4 changes: 2 additions & 2 deletions wp-discourse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -34,7 +34,7 @@
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';
Expand Down