Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Ensure patterns written to the filesystem in unit tests don't persist #185

Merged
merged 58 commits into from
Jun 1, 2023
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 May 17, 2023
c35b2be
Use preg_replace() to update the slug
kienstra May 17, 2023
293e24a
Fix the conversion, using addslashes
kienstra May 17, 2023
7b4ab6a
Add a helper to get whether a pattern has a pattern block
kienstra May 17, 2023
1f48cb8
Add a function to update all patterns
kienstra May 17, 2023
e883ce6
Fix fatal error
kienstra May 17, 2023
1437e52
Allow the slug to be different from the name
kienstra May 17, 2023
68aeef4
Set the slug to the name for new patterns
kienstra May 17, 2023
5994762
For Patternception, don't rely on the name
kienstra May 17, 2023
2b92ef4
If the name changes, change the slug
kienstra May 17, 2023
0b03748
Simplify the name to $name_changed
kienstra May 17, 2023
bafb3bf
Fix a failed PHPUnit test
kienstra May 17, 2023
a1abea9
Add a comment about why the slug is set to the name
kienstra May 17, 2023
35e3e49
Replace ... with array_merge()
kienstra May 17, 2023
6c45631
Pass all 3 arguments to update_slug()
kienstra May 17, 2023
11fb47a
Rename converToSlug(), as it's actually converting to a name
kienstra May 17, 2023
237cb35
Actually rename a file to toKebabCase.ts
kienstra May 17, 2023
cfb3814
Remove an extra 'a'
kienstra May 17, 2023
0e80e0c
Allow saving a new pattern with only meta
kienstra May 17, 2023
a722e10
Fix how name changes work
kienstra May 17, 2023
082122f
Merge in update/patternception-saving-bug, resolve conflict
kienstra May 17, 2023
ccb273b
Use the previous pattern slug
kienstra May 17, 2023
8b4f516
Make a test case cover 2 of the same pattern being renamed
kienstra May 17, 2023
5221859
Test there being no content
kienstra May 17, 2023
2bfc017
Update comments
kienstra May 17, 2023
c25f236
Handle multiple attributes in pattern block
kienstra May 17, 2023
586eced
Add another test case for changing both patterns
kienstra May 17, 2023
5734314
Rename to something without a slash
kienstra May 17, 2023
44cac0e
Allow a pattern slug to start with a number
kienstra May 18, 2023
fb14449
Still handle multiple attributes
kienstra May 18, 2023
eacceff
Update the expected value
kienstra May 18, 2023
cdf3b2b
Prefix a pattern slug with the textdomain
kienstra May 18, 2023
cdba6c6
Fix linting error, remove needless dataProvider
kienstra May 18, 2023
3491301
Fix a comment to refer to the textdomain
kienstra May 18, 2023
0b65850
WIP test
johnstonphilip May 19, 2023
a2c73da
Merge branch 'main' of https://github.com/studiopress/pattern-manager…
johnstonphilip May 22, 2023
7655913
Add tests for scenarios in question
johnstonphilip May 22, 2023
c3bf106
Modify comment
johnstonphilip May 22, 2023
3bc2d3c
Merge branch 'main' of https://github.com/studiopress/pattern-manager…
johnstonphilip May 22, 2023
8c8d9c5
Linting Fixes
johnstonphilip May 22, 2023
f7986f2
Update wp-modules/editor/tests/ModelTest.php
johnstonphilip May 23, 2023
348bb7e
Update wp-modules/editor/tests/ModelTest.php
johnstonphilip May 23, 2023
dbf43d6
Update wp-modules/editor/tests/ModelTest.php
johnstonphilip May 23, 2023
c9089fd
Comment tweak
johnstonphilip May 23, 2023
5c07334
Merge pull request #183 from studiopress/update/patternception-saving…
johnstonphilip May 23, 2023
621d151
Update a Patternception slug in all patterns on changing a slug (#175)
kienstra May 23, 2023
20a84e2
Merge in target branch, resolve merge conflicts
kienstra May 23, 2023
13d64c2
Add space to the bottom of package.json
kienstra May 23, 2023
019e781
Fix unit tests for prefixing
kienstra May 23, 2023
e951357
Only prepend a textdomain if there's a textdomain
kienstra May 23, 2023
23e03b4
Only output the name and textdomain if they exist
kienstra May 23, 2023
6273ca2
Ensure the patterns written to the fs don't persist across tests
kienstra May 23, 2023
0ea824c
Use WP_UnitTestCase factory to create posts
kienstra May 24, 2023
3229372
Remove a filter created for the unit tests
kienstra May 24, 2023
f863b6b
Add a comment back in
kienstra May 24, 2023
ac955a1
Prepend the textdomain to the slug (#180)
kienstra May 24, 2023
be86a4c
Merge in target branch, resolve conflict
kienstra May 24, 2023
832e017
Merge in main, resolve conflicts
kienstra May 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 68 additions & 50 deletions wp-modules/editor/tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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' ] );
Copy link
Contributor Author

@kienstra kienstra May 24, 2023

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.

}

/**
* @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.
*/
Expand Down Expand Up @@ -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(),
]
);
Copy link
Contributor Author

@kienstra kienstra May 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll revert this if you'd like. It's a tangent.

Copy link
Contributor

@johnstonphilip johnstonphilip Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it still works it's fine with me to keep :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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'] );
}

/**
Expand All @@ -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'] );
Expand Down