From 0b6585085985691e9455ab1fadbf600902518a6d Mon Sep 17 00:00:00 2001 From: Phil Johnston Date: Fri, 19 May 2023 15:48:08 -0400 Subject: [PATCH 1/8] WIP test --- wp-modules/editor/tests/ModelTest.php | 31 ++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/wp-modules/editor/tests/ModelTest.php b/wp-modules/editor/tests/ModelTest.php index 579d2445..a2102e30 100644 --- a/wp-modules/editor/tests/ModelTest.php +++ b/wp-modules/editor/tests/ModelTest.php @@ -81,4 +81,33 @@ public function test_delete_pattern_posts_correct_post() { get_posts( [ 'post_type' => 'pm_pattern ' ] ) ); } -} + + /** + * Tests delete_pattern_posts. + */ + 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 = 'page'; + $post->filter = 'raw'; + + // Convert to WP_Post object. + $wp_post = new \WP_Post( $post ); + + // Pass that mocked post into the function we want to test. + save_pattern_to_file( $wp_post ); + + // Get the contents of the file that was saved. + $pattern = get_pattern_by_name( $post->post_name ); + + print_r( $pattern ); + } +} \ No newline at end of file From 76559135b9dbd7ed92e300eaa76a5544f2f54c08 Mon Sep 17 00:00:00 2001 From: Phil Johnston Date: Mon, 22 May 2023 16:13:27 -0400 Subject: [PATCH 2/8] Add tests for scenarios in question --- wp-modules/editor/tests/ModelTest.php | 84 +++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 6 deletions(-) diff --git a/wp-modules/editor/tests/ModelTest.php b/wp-modules/editor/tests/ModelTest.php index a2102e30..7e7a2b22 100644 --- a/wp-modules/editor/tests/ModelTest.php +++ b/wp-modules/editor/tests/ModelTest.php @@ -83,7 +83,7 @@ public function test_delete_pattern_posts_correct_post() { } /** - * Tests delete_pattern_posts. + * Tests that a pattern newly created with Pattern Manager results in a slug that matches the title. */ public function test_new_pattern_title_matches_slug() { @@ -96,18 +96,90 @@ public function test_new_pattern_title_matches_slug() { $post->post_content = 'test pattern content'; $post->post_status = 'publish'; $post->post_name = 'new-pattern-originally-created-with-pattern-manager'; - $post->post_type = 'page'; + $post->post_type = get_pattern_post_type(); $post->filter = 'raw'; // Convert to WP_Post object. $wp_post = new \WP_Post( $post ); - // Pass that mocked post into the function we want to test. - save_pattern_to_file( $wp_post ); + // 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 = get_pattern_by_name( $post->post_name ); + $pattern = \PatternManager\PatternDataHandlers\get_pattern_by_name( $post->post_name ); + + // Make sure the slug of the post and the slug in the file match. + $this->assertSame($post->post_name, $pattern['slug']); + + // Make sure the 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. + $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']; + $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 ); - print_r( $pattern ); + // 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']); + + // 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']); + } } \ No newline at end of file From c3bf106abd09ec7790a31d1e869bb95648d19367 Mon Sep 17 00:00:00 2001 From: Phil Johnston Date: Mon, 22 May 2023 16:14:51 -0400 Subject: [PATCH 3/8] Modify comment --- wp-modules/editor/model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-modules/editor/model.php b/wp-modules/editor/model.php index 0d6c03c1..81251904 100644 --- a/wp-modules/editor/model.php +++ b/wp-modules/editor/model.php @@ -44,7 +44,7 @@ function populate_pattern_from_file( $post ) { add_action( 'the_post', __NAMESPACE__ . '\populate_pattern_from_file' ); /** - * Saves the pattern to the .php file. + * Saves the pattern to the .php file, and also removes the mocked post required for the WP editing UI. * * @param int $post_id The post ID. * @param WP_Post $post The post. From 8c8d9c509c503b685d48a49eb094a922e32a5ae9 Mon Sep 17 00:00:00 2001 From: Phil Johnston Date: Mon, 22 May 2023 16:27:31 -0400 Subject: [PATCH 4/8] Linting Fixes --- wp-modules/editor/tests/ModelTest.php | 116 +++++++++++++------------- 1 file changed, 57 insertions(+), 59 deletions(-) diff --git a/wp-modules/editor/tests/ModelTest.php b/wp-modules/editor/tests/ModelTest.php index 7e7a2b22..e7e6ac5f 100644 --- a/wp-modules/editor/tests/ModelTest.php +++ b/wp-modules/editor/tests/ModelTest.php @@ -81,105 +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 that matches the title. */ 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'; + $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 ); - + // 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 slug of the post and the slug in the file match. - $this->assertSame($post->post_name, $pattern['slug']); - - // Make sure the slug of the post and the filename match. - $this->assertSame($post->post_name, $pattern['name']); + $this->assertSame( $post->post_name, $pattern['slug'] ); + // Make sure the 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'; + $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'; + $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; + + // 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. - $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']; - $post->post_type = get_pattern_post_type(); - $post->filter = 'raw'; - + $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 = $pattern['title']; + $post->post_content = 'This is modified pattern content, but nothing else is changed!'; + $post->post_status = 'publish'; + $post->post_name = $pattern['slug']; + $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; + + // 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']); - + $this->assertSame( 'this-slug-remains-the-same-after-content-changes', $content_modified_pattern['slug'] ); + // 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']); + $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'] ); } -} \ No newline at end of file +} From f7986f2aaf0517cd620959e06595cd28a3d9e0c7 Mon Sep 17 00:00:00 2001 From: Phil Johnston Date: Tue, 23 May 2023 13:10:12 -0400 Subject: [PATCH 5/8] Update wp-modules/editor/tests/ModelTest.php Co-authored-by: Ryan Kienstra --- wp-modules/editor/tests/ModelTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-modules/editor/tests/ModelTest.php b/wp-modules/editor/tests/ModelTest.php index e7e6ac5f..6138909e 100644 --- a/wp-modules/editor/tests/ModelTest.php +++ b/wp-modules/editor/tests/ModelTest.php @@ -108,7 +108,7 @@ public function test_new_pattern_title_matches_slug() { // Get the contents of the file that was saved. $pattern = \PatternManager\PatternDataHandlers\get_pattern_by_name( $post->post_name ); - // Make sure the slug of the post and the slug in the file match. + // Make sure the name of the post and the slug in the file match. $this->assertSame( $post->post_name, $pattern['slug'] ); // Make sure the slug of the post and the filename match. From 348bb7ea0a5dc099afb16166f11d2676e45f690f Mon Sep 17 00:00:00 2001 From: Phil Johnston Date: Tue, 23 May 2023 13:11:01 -0400 Subject: [PATCH 6/8] Update wp-modules/editor/tests/ModelTest.php Co-authored-by: Ryan Kienstra --- wp-modules/editor/tests/ModelTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-modules/editor/tests/ModelTest.php b/wp-modules/editor/tests/ModelTest.php index 6138909e..eda2e574 100644 --- a/wp-modules/editor/tests/ModelTest.php +++ b/wp-modules/editor/tests/ModelTest.php @@ -83,7 +83,7 @@ public function test_delete_pattern_posts_correct_post() { } /** - * Tests that a pattern newly created with Pattern Manager results in a slug that matches the title. + * Tests that a pattern newly created with Pattern Manager results in a slug that matches the name. */ public function test_new_pattern_title_matches_slug() { From dbf43d6377f56a710e47911ad68fa197c7f4dd8e Mon Sep 17 00:00:00 2001 From: Phil Johnston Date: Tue, 23 May 2023 13:22:13 -0400 Subject: [PATCH 7/8] Update wp-modules/editor/tests/ModelTest.php Co-authored-by: Ryan Kienstra --- wp-modules/editor/tests/ModelTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-modules/editor/tests/ModelTest.php b/wp-modules/editor/tests/ModelTest.php index eda2e574..804d4965 100644 --- a/wp-modules/editor/tests/ModelTest.php +++ b/wp-modules/editor/tests/ModelTest.php @@ -111,7 +111,7 @@ public function test_new_pattern_title_matches_slug() { // Make sure the name of the post and the slug in the file match. $this->assertSame( $post->post_name, $pattern['slug'] ); - // Make sure the slug of the post and the filename match. + // Make sure the name of the post and the filename match. $this->assertSame( $post->post_name, $pattern['name'] ); } From c9089fd4c455f2fa313fe607d3397f8411d7df11 Mon Sep 17 00:00:00 2001 From: Phil Johnston Date: Tue, 23 May 2023 13:31:52 -0400 Subject: [PATCH 8/8] Comment tweak --- wp-modules/editor/tests/ModelTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/wp-modules/editor/tests/ModelTest.php b/wp-modules/editor/tests/ModelTest.php index 804d4965..416444e1 100644 --- a/wp-modules/editor/tests/ModelTest.php +++ b/wp-modules/editor/tests/ModelTest.php @@ -83,7 +83,7 @@ public function test_delete_pattern_posts_correct_post() { } /** - * Tests that a pattern newly created with Pattern Manager results in a slug that matches the name. + * 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() { @@ -108,10 +108,10 @@ public function test_new_pattern_title_matches_slug() { // Get the contents of the file that was saved. $pattern = \PatternManager\PatternDataHandlers\get_pattern_by_name( $post->post_name ); - // Make sure the name of the post and the slug in the file match. + // 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 name of the post and the filename match. + // Make sure the post_name (aka "slug") of the post and the filename match. $this->assertSame( $post->post_name, $pattern['name'] ); }