Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command/39 checkPostExists from any CPT #61

Merged
merged 9 commits into from
Nov 16, 2022
Merged

Command/39 checkPostExists from any CPT #61

merged 9 commits into from
Nov 16, 2022

Conversation

faisal-alvi
Copy link
Member

Description of the Change

Added a new command checkPostExists which checks whether a given post title exists in a specified post type or not.

It accepts the following parameters.

  • title: Post Title.
  • postType: Post type. Default to post.

Note: The command searches for the exact title on the site. It fails the test if the title string does not match.

Closes #39

Verification Process

  1. Build the project using npm run build.
  2. Make sure the docker is running.
  3. Run npm run env:start.
  4. Run npm run cypress:open.
  5. Run check-post-exists.test.js.
  6. Try to change the post title(s) in the /tests/cypress/integration/check-post-exists.test.js file and see the test should fail.

Checklist:

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests passed.

Changelog Entry

Added - New Command checkPostExists to check if post exists.

Credits

Props @faisal-alvi

@faisal-alvi faisal-alvi added this to the Future Release milestone Apr 27, 2022
@faisal-alvi faisal-alvi requested a review from a team April 27, 2022 10:54
@faisal-alvi faisal-alvi self-assigned this Apr 27, 2022
@faisal-alvi faisal-alvi requested review from Sidsector9 and removed request for a team April 27, 2022 10:54
Copy link
Member

@Sidsector9 Sidsector9 left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

@faisal-alvi faisal-alvi linked an issue Apr 27, 2022 that may be closed by this pull request
3 tasks
@cadic
Copy link
Contributor

cadic commented Apr 27, 2022

Potentially chainable command. IMO, we should wrap the result and don't fail if the post doesn't exist.

@Sidsector9
Copy link
Member

@faisal-alvi I have a suggestion to make this command run faster, effectively running tests faster.

The admin page exposes the wp-json/wp/v2/posts endpoint which we can use to search for a post(s).
Instead of typing the search term in the search box, submitting and waiting for thee page to reload, we can use REST to search faster:

fetch( `${Cypress.config().baseUrl}/wp-json/wp/v2/post?s="${searchString}"` )
    .then( response => {
        return response.json();
    } )
    .then( data => {
        // Run assertions here.
    } );

Let me know what you think of this. Thanks!

# Conflicts:
#	src/index.ts
@faisal-alvi
Copy link
Member Author

@Sidsector9 thanks for the suggestion, however, I can see only one drawback of using the REST API. It won't work with the CPTs in which the REST API support is disabled, i.e. show_in_rest is set to false.

@faisal-alvi
Copy link
Member Author

@cadic thanks for the suggestion, the result is now wrapped and returned.

@faisal-alvi faisal-alvi requested a review from Sidsector9 June 10, 2022 11:08
@Sidsector9
Copy link
Member

Fair point @faisal-alvi

Although, we can still have the best of both worlds:

  1. Check via REST.
  2. If REST is disabled for a CPT, then use UI

@cadic
Copy link
Contributor

cadic commented Jun 13, 2022

@faisal-alvi thank you for the command. I've added few tests to cover the functionality in both situations (post exists or not)

@faisal-alvi
Copy link
Member Author

@cadic any thoughts on @Sidsector9's ^recommendation?

@cadic cadic merged commit 6479371 into trunk Nov 16, 2022
@cadic cadic deleted the command/39 branch November 16, 2022 11:04
github-actions bot pushed a commit that referenced this pull request Nov 16, 2022
* Command/39 `checkPostExists` from any CPT

* adding examples in documentation

* command/39 wrapping the result and return

* Add real tests

* Activate Classic Editor

* test name fix

* Use block editor to create test posts

* Ignore WP 5.2 Synchronous XHR error

Co-authored-by: Faisal Alvi <[email protected]>
Co-authored-by: Max Lyuchin <[email protected]>
@jeffpaul jeffpaul modified the milestones: Future Release, 0.1.0 Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

checkPostExists from any CPT Migrate custom commands from 10up/publisher-media-kit
4 participants