Repository navigation
Revisions: Store the restored meta in the revision of a restore #16314
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
Open
adamsilverstein
wants to merge
2
commits into
WordPress:trunk
Choose a base branch
from
adamsilverstein:fix/66859-restore-revision-stale-meta
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { test, expect } from '../fixtures'; | ||
|
|
||
| const FOOTNOTE_ID = '3a7c5f0e-8d1b-4f6a-9c2e-5b4d7e8f9a01'; | ||
|
|
||
| /** | ||
| * Builds the content and the footnotes meta of a post with one footnote. | ||
| * | ||
| * @param {string} text Text of the paragraph. | ||
| * @param {string} footnote Text of the footnote. | ||
| * @return {{content: string, meta: {footnotes: string}}} Post data. | ||
| */ | ||
| function postWithFootnote( text, footnote ) { | ||
| return { | ||
| content: `<!-- wp:paragraph --> | ||
| <p>${ text }<sup data-fn="${ FOOTNOTE_ID }" class="fn"><a href="#${ FOOTNOTE_ID }" id="${ FOOTNOTE_ID }-link">1</a></sup></p> | ||
| <!-- /wp:paragraph --> | ||
|
|
||
| <!-- wp:footnotes /-->`, | ||
| meta: { | ||
| footnotes: JSON.stringify( [ | ||
| { id: FOOTNOTE_ID, content: footnote }, | ||
| ] ), | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the footnotes stored in a post or in a revision. | ||
| * | ||
| * @param {{meta: {footnotes: string}}} record Post or revision, as returned by the REST API. | ||
| * @return {string[]} Text of each footnote. | ||
| */ | ||
| function footnotesOf( record ) { | ||
| return JSON.parse( record.meta.footnotes || '[]' ).map( | ||
| ( footnote ) => footnote.content | ||
| ); | ||
| } | ||
|
|
||
| test.describe( 'Restoring a revision of a post with footnotes', () => { | ||
| test( 'stores the restored footnotes in the new revision', async ( { | ||
| page, | ||
| admin, | ||
| postFactory, | ||
| requestUtils, | ||
| } ) => { | ||
| const post = await postFactory.create( { | ||
| title: postFactory.uniqueTitle( 'Restore footnotes' ), | ||
| status: 'publish', | ||
| } ); | ||
| const postPath = `/wp/v2/posts/${ post.id }`; | ||
|
|
||
| // Two saves, so that a revision holds each version of the footnote. | ||
| for ( const data of [ | ||
| postWithFootnote( 'First text.', 'First footnote' ), | ||
| postWithFootnote( 'Second text.', 'Second footnote' ), | ||
| ] ) { | ||
| await requestUtils.rest( { method: 'POST', path: postPath, data } ); | ||
| } | ||
|
|
||
| const revisionsBefore = await requestUtils.rest( { | ||
| path: `${ postPath }/revisions`, | ||
| params: { context: 'edit' }, | ||
| } ); | ||
| const firstRevision = revisionsBefore.find( ( revision ) => | ||
| revision.content.raw.includes( 'First text.' ) | ||
| ); | ||
| expect( footnotesOf( firstRevision ) ).toEqual( [ 'First footnote' ] ); | ||
|
|
||
| await admin.visitAdminPage( | ||
| 'revision.php', | ||
| `revision=${ firstRevision.id }` | ||
| ); | ||
| await page | ||
| .getByRole( 'button', { name: 'Restore This Revision' } ) | ||
| .click(); | ||
| await page.waitForURL( /post\.php\?.*message=5/ ); | ||
|
|
||
| const restoredPost = await requestUtils.rest( { | ||
| path: postPath, | ||
| params: { context: 'edit' }, | ||
| } ); | ||
| expect( restoredPost.content.raw ).toContain( 'First text.' ); | ||
| expect( footnotesOf( restoredPost ) ).toEqual( [ 'First footnote' ] ); | ||
|
|
||
| const revisionsAfter = await requestUtils.rest( { | ||
| path: `${ postPath }/revisions`, | ||
| params: { context: 'edit' }, | ||
| } ); | ||
| expect( revisionsAfter ).toHaveLength( revisionsBefore.length + 1 ); | ||
|
|
||
| // The restore is recorded as a new revision, which has to match the post. | ||
| const latestRevision = revisionsAfter.reduce( ( latest, revision ) => | ||
| revision.id > latest.id ? revision : latest | ||
| ); | ||
| expect( latestRevision.content.raw ).toContain( 'First text.' ); | ||
| expect( footnotesOf( latestRevision ) ).toEqual( [ 'First footnote' ] ); | ||
| } ); | ||
| } ); |
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.