-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Save autoshare meta from post form before publishing tweet #82
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
/** | ||
* Tests functions admin/post-meta.php. | ||
* | ||
* @since 1.0.0 | ||
* @package TenUp\AutoshareForTwitter | ||
*/ | ||
|
||
namespace TenUp\AutoshareForTwitter\Tests; | ||
|
||
use WP_UnitTestCase; | ||
|
||
use const TenUp\AutoshareForTwitter\Core\Post_Meta\ENABLE_AUTOSHARE_FOR_TWITTER_KEY; | ||
use const TenUp\AutoshareForTwitter\Core\Post_Meta\TWITTER_STATUS_KEY; | ||
|
||
use function TenUp\AutoshareForTwitter\Core\Post_Meta\get_tweet_status_message; | ||
use function TenUp\AutoshareForTwitter\Core\Post_Transition\maybe_publish_tweet; | ||
|
||
/** | ||
* TestPostTransition | ||
* | ||
* @group post_transition | ||
* | ||
* @sincd 1.0.0 | ||
*/ | ||
class TestPostTransition extends WP_UnitTestCase { | ||
/** | ||
* Setup. | ||
*/ | ||
public function setUp() { | ||
wp_set_current_user( 1 ); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
/** | ||
* Provides test data. | ||
* | ||
* @return array | ||
*/ | ||
public function maybe_publish_tweet_provider() { | ||
return [ | ||
[ | ||
// Post transitioning from publish to draft should not tweet. | ||
[ 'post_status' => 'publish' ], | ||
'1', | ||
'draft', | ||
'publish', | ||
false, | ||
], | ||
[ | ||
// Already-published post should not tweet. | ||
[ 'post_status' => 'publish' ], | ||
'1', | ||
'publish', | ||
'publish', | ||
false, | ||
], | ||
[ | ||
// Post transitioning from draft to publish should tweet if autotweet_enabled is true. | ||
[ | ||
'post_status' => 'draft', | ||
'post_title' => 'TEST', | ||
'post_author' => 1, | ||
], | ||
'1', | ||
'publish', | ||
'draft', | ||
true, | ||
], | ||
[ | ||
// Post transitioning from draft to publish should not tweet if autotweet is not true. | ||
[ 'post_status' => 'draft' ], | ||
'0', | ||
'publish', | ||
'draft', | ||
false, | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Tests the maybe_publish_tweet function. | ||
* | ||
* @dataProvider maybe_publish_tweet_provider | ||
* | ||
* @param array $post_args Args to pass to the create post function. | ||
* @param boolean $autoshare_enabled_form_data Updated autoshare enabled meta value. | ||
* @param string $new_status The new post status. | ||
* @param string $old_status The old post status. | ||
* @param boolean $expected_should_tweet Whether the post should be tweeted. | ||
*/ | ||
public function test_maybe_publish_tweet( | ||
$post_args, | ||
$autoshare_enabled_form_data, | ||
$new_status, | ||
$old_status, | ||
$expected_should_tweet | ||
) { | ||
$the_post = $this->factory->post->create_and_get( $post_args ); | ||
$post_was_tweeted = false; | ||
$pre_status_update_callback = function() use ( &$post_was_tweeted ) { | ||
$post_was_tweeted = true; | ||
|
||
// Minimum valid response. | ||
return (object) [ | ||
'id' => 1, | ||
'created_at' => time(), | ||
]; | ||
}; | ||
add_filter( 'autoshare_for_twitter_pre_status_update', $pre_status_update_callback ); | ||
|
||
$post_form_data_callback = function() use ( $autoshare_enabled_form_data ) { | ||
return [ ENABLE_AUTOSHARE_FOR_TWITTER_KEY => $autoshare_enabled_form_data ]; | ||
}; | ||
add_filter( 'autoshare_post_form_data', $post_form_data_callback ); | ||
|
||
maybe_publish_tweet( $new_status, $old_status, $the_post ); | ||
|
||
if ( $expected_should_tweet ) { | ||
$this->assertTrue( $post_was_tweeted ); | ||
} else { | ||
$this->assertFalse( $post_was_tweeted ); | ||
} | ||
|
||
remove_filter( 'autoshare_for_twitter_pre_status_update', $pre_status_update_callback ); | ||
remove_filter( 'autoshare_post_form_data', $post_form_data_callback ); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Broke this out into a function (see below) for easier testing.