-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
71e12d5
commit 2751e0a
Showing
20 changed files
with
768 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
<?php | ||
use PHPUnit\Framework\Assert; | ||
/* | ||
|-------------------------------------------------------------------------- | ||
| Test Case | ||
|-------------------------------------------------------------------------- | ||
| | ||
| The closure you provide to your test functions is always bound to a specific PHPUnit test | ||
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may | ||
| need to change it using the "uses()" function to bind a different classes or traits. | ||
| | ||
*/ | ||
|
||
// uses(Tests\TestCase::class)->in('Feature'); | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Global hooks | ||
|-------------------------------------------------------------------------- | ||
|*/ | ||
|
||
uses() | ||
->beforeAll( | ||
function() { | ||
// Make sure all required plugins are active | ||
$required_plugins = array( | ||
// 'akismet/akismet.php', | ||
'duplicate-post/duplicate-post.php', | ||
'gravityforms/gravityforms.php', | ||
); | ||
|
||
foreach ($required_plugins as $plugin) { | ||
if (!is_plugin_active($plugin)) { | ||
throw new Exception("Plugin `{$plugin}` is not installed/active"); | ||
} | ||
} | ||
|
||
// Chcek table exists | ||
global $wpdb; | ||
$table = NASHAAT_DB_TABLE; | ||
$result = $wpdb->query("Select 1 from {$table} LIMIT 1"); | ||
|
||
if ($result === false) { | ||
throw new Exception("Table `{$table}` doesn't exists"); | ||
} | ||
|
||
wp_clear_auth_cookie(); | ||
wp_set_current_user(1); | ||
wp_set_auth_cookie(1); | ||
}) | ||
->afterAll(function() { | ||
// Clear table | ||
global $wpdb; | ||
$table = NASHAAT_DB_TABLE; | ||
$wpdb->query("TRUNCATE TABLE {$table}"); | ||
|
||
wp_clear_auth_cookie(); | ||
wp_set_current_user(0); | ||
wp_set_auth_cookie(0); | ||
|
||
})->in(__DIR__); | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Expectations | ||
|-------------------------------------------------------------------------- | ||
| | ||
| When you're writing tests, you often need to check that values meet certain conditions. The | ||
| "expect()" function gives you access to a set of "expectations" methods that you can use | ||
| to assert different things. Of course, you may extend the Expectation API at any time. | ||
| | ||
*/ | ||
|
||
|
||
/** | ||
* Callback for toHaveProperties. | ||
* | ||
* This has been abstracted out so that it can be called recursively. | ||
* | ||
* @param iterable<array-key, mixed> $incoming The incoming array | ||
* @param iterable<array-key, mixed> $expected The expected array | ||
* @param string $message The message to display if the assertion fails | ||
*/ | ||
function assert_object(mixed $incoming, iterable $expected, string $message): void { | ||
|
||
$incoming_array = is_object($incoming) && method_exists($incoming, 'toArray') ? $incoming->toArray() : (array) $incoming; | ||
// $expected_array = is_object($expected) && method_exists($expected, 'toArray') ? $expected->toArray() : (array) $expected; | ||
|
||
foreach ($expected as $name => $value) { | ||
// Check if the key from $expected exists in $incoming | ||
$key = is_int($name) && (is_string($value) || is_int($value)) ? $value : $name; | ||
$non_existent = $message; | ||
if ($non_existent === '') { | ||
$non_existent = "Failed asserting that `{$key}` exists"; | ||
} | ||
|
||
if (array_is_list($incoming_array)) { | ||
Assert::assertTrue(in_array($key, $incoming_array), $non_existent); | ||
} else { | ||
Assert::assertTrue(array_key_exists($key, $incoming_array), $non_existent); | ||
} | ||
|
||
|
||
$incoming_value = $incoming_array[$key]; | ||
// if $value is an iterable, recurse | ||
if (is_iterable($value)) { | ||
assert_object($incoming_value, $value, $message); | ||
|
||
continue; | ||
} | ||
|
||
// $name exists and it is not an int (not a numeric key) | ||
// so we can check against $value | ||
if (!is_int($name)) { | ||
Assert::assertEquals($value, $incoming_value, $message); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Extend expect to check one object against another. | ||
* This is similar to `toHaveProperties` but it is recursive. | ||
*/ | ||
expect()->extend('toIncludeProperties', function(iterable $expected, $message = '') { | ||
|
||
assert_object($this->value, $expected, $message); | ||
|
||
return $this; | ||
}); | ||
|
||
// expect()->intercept('toHaveProperties', 'iterable', function(iterable $expected, bool $ignore_case = false) { | ||
// // expect($this->value->id)->toBe($expected->id); | ||
// foreach ($expected as $name => $value) { | ||
// if (is_array($value)) { | ||
// return $this->toHaveProperties($value); | ||
// continue; | ||
// } | ||
|
||
// return is_int($name) ? $this->toHaveProperty($value) : $this->toHaveProperty($name, $value); | ||
// } | ||
|
||
|
||
// echo $ignore_case; | ||
// }); | ||
|
||
// expect()->pipe('toHaveProperties', function(Closure $next, iterable $expected) { | ||
// foreach ($expected as $name => $value) { | ||
// if (is_array($value)) { | ||
// return $this->toHaveProperties($value); | ||
// continue; | ||
// } | ||
|
||
// return is_int($name) ? $this->toHaveProperty($value) : $this->toHaveProperty($name, $value); | ||
// } | ||
|
||
// // if ($this->value instanceof Model) { | ||
// // return expect($this->value->id)->toBe($expected->id); | ||
// // } | ||
|
||
// // return $next(); // Run to the original, built-in expectation... | ||
// }); | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Functions | ||
|-------------------------------------------------------------------------- | ||
| | ||
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your | ||
| project that you don't want to repeat in every file. Here you can also expose helpers as | ||
| global functions to help you to reduce the number of lines of code in your test files. | ||
| | ||
*/ | ||
|
||
/** | ||
* | ||
*/ | ||
function something() { | ||
// .. | ||
} |
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,71 @@ | ||
<?php | ||
|
||
test('Comments', function() { | ||
$comment = array( | ||
'comment_ID' => '1', | ||
'comment_post_ID' => '1', | ||
'comment_author' => 'admin', | ||
'comment_date' => '2019-01-01 00:00:00', | ||
'user_id' => '1', | ||
); | ||
|
||
do_action('edit_comment', 1, $comment); | ||
$data = get_data(); | ||
expect($data)->toIncludeProperties(array( | ||
'event' => 'edited', | ||
'context' => 'comment', | ||
'log_info' => array( | ||
'post_id' => '1', | ||
'post_type' => 'post', | ||
'id' => '1', | ||
), | ||
)); | ||
|
||
assert_snapshot($data, 'comment-edited'); | ||
|
||
|
||
$comment_object = new WP_Comment((object) $comment); | ||
|
||
do_action('transition_comment_status', 'approved', '', $comment_object); | ||
$data = get_data(); | ||
expect($data)->toIncludeProperties(array( | ||
'event' => 'approved', | ||
'context' => 'comment', | ||
'log_info' => array( | ||
'post_id' => '1', | ||
'post_type' => 'post', | ||
'id' => '1', | ||
), | ||
)); | ||
assert_snapshot($data, 'comment-status'); | ||
|
||
do_action('transition_comment_status', 'unapproved', '', $comment_object); | ||
$data = get_data(); | ||
expect($data)->toIncludeProperties(array( | ||
'event' => 'unapproved', | ||
'context' => 'comment', | ||
'log_info' => array( | ||
'post_id' => '1', | ||
'post_type' => 'post', | ||
'id' => '1', | ||
), | ||
)); | ||
assert_snapshot($data, 'comment-status'); | ||
|
||
// Since no action is taken, get_data should return | ||
// the last action which is unapproved | ||
do_action('transition_comment_status', 'same', 'same', $comment_object); | ||
expect($data)->toIncludeProperties(array( | ||
'event' => 'unapproved', | ||
'context' => 'comment', | ||
'log_info' => array( | ||
'post_id' => '1', | ||
'post_type' => 'post', | ||
'id' => '1', | ||
), | ||
)); | ||
|
||
assert_snapshot($data, 'comment-status'); | ||
|
||
|
||
}); |
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,76 @@ | ||
<?php | ||
test('Duplicate hooks', function() { | ||
$post['ID'] = 1; | ||
$post['post_title'] = 'test'; | ||
$post['post_type'] = 'post'; | ||
|
||
$prev_post = new WP_Post((object) $post); | ||
|
||
do_action('dp_duplicate_post', 2, $prev_post, 'draft'); | ||
expect(get_data())->toIncludeProperties(array( | ||
'event' => 'cloned', | ||
'context' => 'duplicate_post', | ||
'log_info' => array( | ||
'new_post_id' => 2, | ||
'prev_post_id' => 1, | ||
'status' => 'draft', | ||
'prev_post_title' => 'test', | ||
'new_post_title' => 'Sample Page', | ||
'post_type' => 'Post', | ||
), | ||
)); | ||
}); | ||
|
||
|
||
test('Duplicate plugin settings', function() { | ||
// Required to set the settings for the pluging | ||
do_action('admin_init'); | ||
|
||
// Action to update the settings | ||
do_action('updated_option', 'duplicate_post_title_prefix', 'old_prefix', 'new_prefix'); | ||
|
||
// Action to log the changes to database | ||
apply_filters('wp_redirect', ''); | ||
|
||
expect(get_data())->toIncludeProperties(array( | ||
'event' => 'updated', | ||
'context' => 'duplicate_post_settings', | ||
'log_info' => array( | ||
'duplicate_post_title_prefix' => array( | ||
"prev" => "old_prefix", | ||
"new" => "new_prefix", | ||
), | ||
), | ||
)); | ||
|
||
|
||
do_action('updated_option', 'duplicate_post_copyauthor', false, true); | ||
do_action('updated_option', 'duplicate_post_copyexcerpt', true, false); | ||
do_action('updated_option', 'duplicate_post_show_original_meta_box', false, true); | ||
apply_filters('wp_redirect', ''); | ||
|
||
|
||
expect(get_data())->toIncludeProperties(array( | ||
'event' => 'updated', | ||
'context' => 'duplicate_post_settings', | ||
'log_info' => array( | ||
'elements-to-copy' => array( | ||
'duplicate_post_copyauthor' => array( | ||
"prev" => false, | ||
"new" => true, | ||
), | ||
'duplicate_post_copyexcerpt' => array( | ||
"prev" => true, | ||
"new" => false, | ||
), | ||
), | ||
'show-original' => array( | ||
'duplicate_post_show_original_meta_box' => array( | ||
"prev" => false, | ||
"new" => true, | ||
), | ||
), | ||
), | ||
)); | ||
expect(true)->toBe(true); | ||
}); |
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,31 @@ | ||
<?php | ||
|
||
|
||
test("Gravity forms", function() { | ||
do_action('gform_post_form_restored', 1); | ||
do_action('gform_post_form_trashed', 1); | ||
do_action('gform_before_delete_form', 1); | ||
do_action('gform_post_form_activated', 1); | ||
do_action('gform_post_form_deactivated', 1); | ||
do_action('gform_post_form_views_deleted', 1); | ||
|
||
$form = array( | ||
'id' => 1, | ||
'title' => 'test', | ||
); | ||
do_action('gform_after_save_form', $form, false); | ||
do_action('gform_after_save_form', $form, true); | ||
|
||
|
||
do_action('gform_post_form_duplicated', 1, 2); | ||
|
||
|
||
// Log import export | ||
do_action('check_admin_referer', 'test'); | ||
do_action('check_admin_referer', 'gf_export_forms'); | ||
|
||
$_POST['export_forms']['gf_form_id'] = array(1, 2, 3); | ||
do_action('check_admin_referer', 'gf_export_forms'); | ||
|
||
expect(true)->toBe(true); | ||
}); |
Oops, something went wrong.