This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Ensure patterns written to the filesystem in unit tests don't persist #185
Merged
Merged
Changes from all commits
Commits
Show all changes
58 commits
Select commit
Hold shift + click to select a range
74073bc
Add a naive function to update a slug
kienstra c35b2be
Use preg_replace() to update the slug
kienstra 293e24a
Fix the conversion, using addslashes
kienstra 7b4ab6a
Add a helper to get whether a pattern has a pattern block
kienstra 1f48cb8
Add a function to update all patterns
kienstra e883ce6
Fix fatal error
kienstra 1437e52
Allow the slug to be different from the name
kienstra 68aeef4
Set the slug to the name for new patterns
kienstra 5994762
For Patternception, don't rely on the name
kienstra 2b92ef4
If the name changes, change the slug
kienstra 0b03748
Simplify the name to $name_changed
kienstra bafb3bf
Fix a failed PHPUnit test
kienstra a1abea9
Add a comment about why the slug is set to the name
kienstra 35e3e49
Replace ... with array_merge()
kienstra 6c45631
Pass all 3 arguments to update_slug()
kienstra 11fb47a
Rename converToSlug(), as it's actually converting to a name
kienstra 237cb35
Actually rename a file to toKebabCase.ts
kienstra cfb3814
Remove an extra 'a'
kienstra 0e80e0c
Allow saving a new pattern with only meta
kienstra a722e10
Fix how name changes work
kienstra 082122f
Merge in update/patternception-saving-bug, resolve conflict
kienstra ccb273b
Use the previous pattern slug
kienstra 8b4f516
Make a test case cover 2 of the same pattern being renamed
kienstra 5221859
Test there being no content
kienstra 2bfc017
Update comments
kienstra c25f236
Handle multiple attributes in pattern block
kienstra 586eced
Add another test case for changing both patterns
kienstra 5734314
Rename to something without a slash
kienstra 44cac0e
Allow a pattern slug to start with a number
kienstra fb14449
Still handle multiple attributes
kienstra eacceff
Update the expected value
kienstra cdf3b2b
Prefix a pattern slug with the textdomain
kienstra cdba6c6
Fix linting error, remove needless dataProvider
kienstra 3491301
Fix a comment to refer to the textdomain
kienstra 0b65850
WIP test
johnstonphilip a2c73da
Merge branch 'main' of https://github.com/studiopress/pattern-manager…
johnstonphilip 7655913
Add tests for scenarios in question
johnstonphilip c3bf106
Modify comment
johnstonphilip 3bc2d3c
Merge branch 'main' of https://github.com/studiopress/pattern-manager…
johnstonphilip 8c8d9c5
Linting Fixes
johnstonphilip f7986f2
Update wp-modules/editor/tests/ModelTest.php
johnstonphilip 348bb7e
Update wp-modules/editor/tests/ModelTest.php
johnstonphilip dbf43d6
Update wp-modules/editor/tests/ModelTest.php
johnstonphilip c9089fd
Comment tweak
johnstonphilip 5c07334
Merge pull request #183 from studiopress/update/patternception-saving…
johnstonphilip 621d151
Update a Patternception slug in all patterns on changing a slug (#175)
kienstra 20a84e2
Merge in target branch, resolve merge conflicts
kienstra 13d64c2
Add space to the bottom of package.json
kienstra 019e781
Fix unit tests for prefixing
kienstra e951357
Only prepend a textdomain if there's a textdomain
kienstra 23e03b4
Only output the name and textdomain if they exist
kienstra 6273ca2
Ensure the patterns written to the fs don't persist across tests
kienstra 0ea824c
Use WP_UnitTestCase factory to create posts
kienstra 3229372
Remove a filter created for the unit tests
kienstra f863b6b
Add a comment back in
kienstra ac955a1
Prepend the textdomain to the slug (#180)
kienstra be86a4c
Merge in target branch, resolve conflict
kienstra 832e017
Merge in main, resolve conflicts
kienstra 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,10 @@ | |
namespace PatternManager\Editor; | ||
|
||
use WP_UnitTestCase; | ||
use function \PatternManager\Editor\save_pattern_to_file; | ||
use function \PatternManager\GetWpFilesystem\get_wp_filesystem_api; | ||
use function \PatternManager\PatternDataHandlers\get_pattern_by_name; | ||
use function \PatternManager\PatternDataHandlers\get_patterns_directory; | ||
|
||
require_once dirname( __DIR__ ) . '/model.php'; | ||
|
||
|
@@ -16,6 +20,35 @@ | |
*/ | ||
class ModelTest extends WP_UnitTestCase { | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function setUp(): void { | ||
parent::setUp(); | ||
$this->stylesheet_dir = get_wp_filesystem_api()->wp_themes_dir() . '/pm-testing'; | ||
get_wp_filesystem_api()->mkdir( $this->stylesheet_dir ); | ||
add_filter( 'stylesheet_directory', [ $this, 'get_stylesheet_dir' ] ); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function tearDown(): void { | ||
remove_filter( 'stylesheet_directory', [ $this, 'get_stylesheet_dir' ] ); | ||
get_wp_filesystem_api()->rmdir( | ||
$this->stylesheet_dir, | ||
true | ||
); | ||
parent::tearDown(); | ||
} | ||
|
||
/** | ||
* Gets the stub stylesheet directory. | ||
*/ | ||
public function get_stylesheet_dir() { | ||
return $this->stylesheet_dir; | ||
} | ||
|
||
/** | ||
* Tests add_active_theme_to_heartbeat. | ||
*/ | ||
|
@@ -88,31 +121,26 @@ public function test_delete_pattern_posts_correct_post() { | |
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 ); | ||
$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. I'll revert this if you'd like. It's a tangent. 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. If it still works it's fine with me to keep :) 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. OK, thanks! |
||
|
||
// Save the mocked pattern to the disk. | ||
\PatternManager\Editor\save_pattern_to_file( $wp_post ); | ||
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 ); | ||
$pattern = get_pattern_by_name( $wp_post->post_name ); | ||
|
||
// Make sure the ->post_name is the same as the slug, other than the prefixed textdomain. | ||
$this->assertStringEndsWith( $post->post_name, $pattern['slug'] ); | ||
$this->assertStringEndsWith( $wp_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'] ); | ||
$this->assertSame( $wp_post->post_name, $pattern['name'] ); | ||
} | ||
|
||
/** | ||
|
@@ -121,55 +149,45 @@ public function test_new_pattern_title_matches_slug() { | |
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 = '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 ); | ||
$wp_post = $this->factory()->post->create_and_get( | ||
[ | ||
'post_title' => 'This title remains the same after content changes.', | ||
'post_content' => 'test pattern content', | ||
'post_name' => 'remains-the-same-after-content-changes', | ||
'post_type' => get_pattern_post_type(), | ||
] | ||
); | ||
|
||
// Save the pattern to the disk. | ||
\PatternManager\Editor\save_pattern_to_file( $wp_post ); | ||
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 = get_wp_filesystem_api(); | ||
$patterns_dir = get_patterns_directory(); | ||
$original_name = $wp_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' ); | ||
$pattern = 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 ); | ||
$wp_post = $this->factory()->post->create_and_get( | ||
[ | ||
'post_title' => $pattern['title'], | ||
'post_content' => 'This is modified pattern content, but nothing else is changed!', | ||
'post_name' => $pattern['name'], | ||
'post_type' => get_pattern_post_type(), | ||
] | ||
); | ||
|
||
// Save the pattern to the disk. | ||
\PatternManager\Editor\save_pattern_to_file( $wp_post ); | ||
save_pattern_to_file( $wp_post ); | ||
|
||
// Get the contents of the file. | ||
$content_modified_pattern = \PatternManager\PatternDataHandlers\get_pattern_by_name( 'mismatched-name' ); | ||
$content_modified_pattern = get_pattern_by_name( 'mismatched-name' ); | ||
|
||
// Make sure the slug does not get changed when only the content is modified. | ||
$this->assertStringEndsWith( 'remains-the-same-after-content-changes', $content_modified_pattern['slug'] ); | ||
|
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.
Like this example, this makes get_patterns_directory() get the mock theme directory we created for these unit tests:
$this->stylesheet_dir
.