-
Notifications
You must be signed in to change notification settings - Fork 6
Update/patternception saving bug tests #183
Changes from all commits
0b65850
a2c73da
7655913
c3bf106
8c8d9c5
f7986f2
348bb7e
dbf43d6
c9089fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -81,4 +81,103 @@ public function test_delete_pattern_posts_correct_post() { | |||||
get_posts( [ 'post_type' => 'pm_pattern ' ] ) | ||||||
); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Tests that a pattern newly created with Pattern Manager results in a slug and filename that matches the post_name. | ||||||
*/ | ||||||
public function test_new_pattern_title_matches_slug() { | ||||||
|
||||||
// Mock a post object so we can test it. | ||||||
$post_id = -998; // negative ID, to avoid clash with a valid post. | ||||||
$post = new \stdClass(); | ||||||
$post->ID = $post_id; | ||||||
$post->post_author = 1; | ||||||
$post->post_title = 'New Pattern, originally created with Pattern Manager.'; | ||||||
$post->post_content = 'test pattern content'; | ||||||
$post->post_status = 'publish'; | ||||||
$post->post_name = 'new-pattern-originally-created-with-pattern-manager'; | ||||||
$post->post_type = get_pattern_post_type(); | ||||||
$post->filter = 'raw'; | ||||||
|
||||||
// Convert to WP_Post object. | ||||||
$wp_post = new \WP_Post( $post ); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sometimes the post factory from $wp_post = $this->factory()->post->create_and_get(
[
'post_title' => 'New Pattern, originally created with Pattern Manager.',
'post_content' => 'test pattern content',
'post_name' => 'new-pattern-originally-created-with-pattern-manager',
'post_type' => get_pattern_post_type(),
]
); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh nice! |
||||||
|
||||||
// Save the mocked pattern to the disk. | ||||||
\PatternManager\Editor\save_pattern_to_file( $wp_post ); | ||||||
|
||||||
// Get the contents of the file that was saved. | ||||||
$pattern = \PatternManager\PatternDataHandlers\get_pattern_by_name( $post->post_name ); | ||||||
|
||||||
// Make sure the post_name (aka "slug") of the post and the slug in the file match. | ||||||
$this->assertSame( $post->post_name, $pattern['slug'] ); | ||||||
|
||||||
// Make sure the post_name (aka "slug") of the post and the filename match. | ||||||
$this->assertSame( $post->post_name, $pattern['name'] ); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Tests a pattern with a slug that does not match the filename remains the same if something other than the title was not changed. | ||||||
*/ | ||||||
public function test_slug_and_filename_stay_the_same_after_content_update() { | ||||||
|
||||||
// Mock a post object so we can test it. | ||||||
$post_id = -997; // negative ID, to avoid clash with a valid post. | ||||||
$post = new \stdClass(); | ||||||
$post->ID = $post_id; | ||||||
$post->post_author = 1; | ||||||
$post->post_title = 'This title remains the same after content changes.'; | ||||||
$post->post_content = 'test pattern content'; | ||||||
$post->post_status = 'publish'; | ||||||
$post->post_name = 'this-slug-remains-the-same-after-content-changes'; | ||||||
$post->post_type = get_pattern_post_type(); | ||||||
$post->filter = 'raw'; | ||||||
|
||||||
// Convert to WP_Post object. | ||||||
$wp_post = new \WP_Post( $post ); | ||||||
|
||||||
// Save the pattern to the disk. | ||||||
\PatternManager\Editor\save_pattern_to_file( $wp_post ); | ||||||
|
||||||
// Rename the pattern's filename to something different than the slug. | ||||||
// This is to mock how it might be for for a non-pm-made pattern, with mismatching filenames and slugs. | ||||||
$wp_filesystem = \PatternManager\GetWpFilesystem\get_wp_filesystem_api(); | ||||||
$patterns_dir = \PatternManager\PatternDataHandlers\get_patterns_directory(); | ||||||
$original_name = $post->post_name . '.php'; | ||||||
$mocked_mismatching_name = 'mismatched-name.php'; | ||||||
$wp_filesystem->move( $patterns_dir . $original_name, $patterns_dir . $mocked_mismatching_name ); | ||||||
$wp_filesystem->delete( $patterns_dir . $original_name ); | ||||||
|
||||||
// Get the contents of the file. | ||||||
$pattern = \PatternManager\PatternDataHandlers\get_pattern_by_name( 'mismatched-name' ); | ||||||
|
||||||
// Mock a post object so we can test modifying only the content. | ||||||
$post_id = -997; // negative ID, to avoid clash with a valid post. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea to mock a new When the filename is changed, the |
||||||
$post = new \stdClass(); | ||||||
$post->ID = $post_id; | ||||||
$post->post_author = 1; | ||||||
$post->post_title = $pattern['title']; | ||||||
$post->post_content = 'This is modified pattern content, but nothing else is changed!'; | ||||||
$post->post_status = 'publish'; | ||||||
$post->post_name = $pattern['slug']; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason you'd set the slug to this as opposed to pulling from the file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a nitpick 😄 But if it were to come from the file, it should be the In tests, I like setting values to string literals, as string variables are mutable. |
||||||
$post->post_type = get_pattern_post_type(); | ||||||
$post->filter = 'raw'; | ||||||
|
||||||
// Convert to WP_Post object. | ||||||
$wp_post = new \WP_Post( $post ); | ||||||
|
||||||
// Save the pattern to the disk. | ||||||
\PatternManager\Editor\save_pattern_to_file( $wp_post ); | ||||||
|
||||||
// Get the contents of the file. | ||||||
$content_modified_pattern = \PatternManager\PatternDataHandlers\get_pattern_by_name( 'mismatched-name' ); | ||||||
|
||||||
// Make sure the slug does not get changed when only the content is modified. | ||||||
$this->assertSame( 'this-slug-remains-the-same-after-content-changes', $content_modified_pattern['slug'] ); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great work figuring out a test for this. Now, we can be more confident in our PRs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really nice naming of |
||||||
|
||||||
// Make sure the title does not get changed when only the content is modified. | ||||||
$this->assertSame( 'This title remains the same after content changes.', $content_modified_pattern['title'] ); | ||||||
|
||||||
// Make sure the filename does not get changed when only the content is modified. | ||||||
$this->assertSame( 'mismatched-name', $content_modified_pattern['name'] ); | ||||||
} | ||||||
} |
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.
Nitpick that shouldn't block merging:
The pattern PHP files written in these tests shouldn't persist into the next tests.
Maybe:
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.
If you'd like, I'll open a PR for this, as it's my nitpick 😄
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.
Yeah if you don't mind, I'd love to fully understand how this works :)
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.
Sure, I'll do that. Still, don't wait to merge this.