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

Update/patternception saving bug tests #183

Conversation

johnstonphilip
Copy link
Contributor

@johnstonphilip johnstonphilip commented May 22, 2023

Tasks

Summary of changes

This is a PR to #177 which simply adds some PHPunit tests for the changes in question there.

@johnstonphilip johnstonphilip changed the base branch from update/patternception-saving-bug to main May 22, 2023 20:15
@johnstonphilip johnstonphilip changed the base branch from main to update/patternception-saving-bug May 22, 2023 20:16
@johnstonphilip
Copy link
Contributor Author

I'm not sure why the diff is showing changes outside phpunit changes, but that's all this one is.

@kienstra
Copy link
Contributor

Thanks, I'll review this today.

Copy link
Contributor

@kienstra kienstra left a comment

Choose a reason for hiding this comment

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

Hi @johnstonphilip,
Really nice work.

Some comments below, but no blocker to merging.

$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'];
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick:

Suggested change
$post->post_name = $pattern['slug'];
$post->post_name = 'mismatched-name';

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

@kienstra kienstra May 23, 2023

Choose a reason for hiding this comment

The 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 $pattern['name'].

In tests, I like setting values to string literals, as string variables are mutable.

$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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Good idea to mock a new WP_Post.

When the filename is changed, the WP_Post in the editor will be different.

$post->filter = 'raw';

// Convert to WP_Post object.
$wp_post = new \WP_Post( $post );
Copy link
Contributor

Choose a reason for hiding this comment

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

Sometimes the post factory from WP_UnitTestCase helps:

		$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

Choose a reason for hiding this comment

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

Oh nice!

/**
* 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() {
Copy link
Contributor

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:

function setUp() {
  // Create a tmp directory
  // $this->stylesheet_dir = created tmp directory
  add_filter( 'stylesheet_directory', [ $this, 'get_stylesheet_dir' ] ); // So update_pattern() will save patterns to the tmp dir, not the actual theme.
}

function tearDown() {
   // Delete $this->stylesheet_dir, maybe with the WP filesystem API
}

function get_stylesheet_dir() {
   return $this->stylesheet_dir;
}

Copy link
Contributor

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 😄

Copy link
Contributor Author

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 :)

Copy link
Contributor

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.

wp-modules/editor/tests/ModelTest.php Outdated Show resolved Hide resolved
$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'] );
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Really nice naming of 'this-slug-remains-the-same-after-content-changes'

wp-modules/editor/tests/ModelTest.php Outdated Show resolved Hide resolved
wp-modules/editor/tests/ModelTest.php Outdated Show resolved Hide resolved
johnstonphilip and others added 4 commits May 23, 2023 13:10
Co-authored-by: Ryan Kienstra <kienstraryan@gmail.com>
Co-authored-by: Ryan Kienstra <kienstraryan@gmail.com>
Co-authored-by: Ryan Kienstra <kienstraryan@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants