PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/post/revisions.php

https://gitlab.com/Blueprint-Marketing/wordpress-unit-tests
PHP | 341 lines | 201 code | 69 blank | 71 comment | 0 complexity | f2cb1c6f8a349849c61d1d9ab5816019 MD5 | raw file
  1. <?php
  2. /**
  3. * @group post
  4. * @group revision
  5. */
  6. class Tests_Post_Revisions extends WP_UnitTestCase {
  7. function setUp() {
  8. parent::setUp();
  9. $this->post_type = rand_str( 20 );
  10. }
  11. function tearDown() {
  12. parent::tearDown();
  13. unset( $GLOBALS['wp_post_types'][ $this->post_type ] );
  14. }
  15. /**
  16. * Note: Test needs reviewing when #16215 is fixed because I'm not sure the test current tests the "correct" behavior
  17. * @ticket 20982
  18. * @ticket 16215
  19. */
  20. function test_revision_restore_updates_edit_last_post_meta() {
  21. $admin_user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
  22. $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
  23. $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) );
  24. //create a post as Author
  25. wp_set_current_user( $author_user_id );
  26. $post = get_default_post_to_edit( 'post', true );
  27. $post_id = $post->ID;
  28. wp_update_post( array( 'post_status' => 'draft', 'post_content' => 'I cant spel werds.', 'ID' => $post_id ) );
  29. //update post as Editor
  30. wp_set_current_user( $editor_user_id );
  31. wp_update_post( array( 'post_content' => 'The Editor was in fixing your typos.', 'ID' => $post_id ) );
  32. //restore back as Admin
  33. wp_set_current_user( $admin_user_id );
  34. $revisions = wp_get_post_revisions( $post->ID );
  35. $this->assertCount( 2, $revisions );
  36. $lastrevision = end( $revisions );
  37. $this->assertEquals( 'I cant spel werds.', $lastrevision->post_content );
  38. // #16215
  39. $this->assertEquals( $author_user_id , $lastrevision->post_author);
  40. wp_restore_post_revision( $lastrevision->ID );
  41. //is post_meta correctly set to revision author
  42. $this->assertEquals( $admin_user_id, get_post_meta( $post_id, '_edit_last', true ) ); //after restoring user
  43. wp_set_current_user( 0 );
  44. }
  45. /**
  46. * @ticket 7392
  47. * @ticket 9843
  48. */
  49. function test_revision_dont_save_revision_if_unchanged() {
  50. $post = get_default_post_to_edit( 'post', true );
  51. $post_id = $post->ID;
  52. $this->assertCount( 0, wp_get_post_revisions( $post_id ) ); // No revisions on auto-draft creation.
  53. wp_update_post( array( 'post_status' => 'draft', 'post_title' => 'some-post', 'post_content' => 'some_content', 'ID' => $post_id ) );
  54. $this->assertCount( 1, wp_get_post_revisions( $post_id ) ); // Just the initial revision
  55. // First update
  56. wp_update_post( array( 'post_content' => 'some updated content', 'ID' => $post_id ) );
  57. $this->assertCount( 2, wp_get_post_revisions( $post_id ) ); // should be 2 revisions so far
  58. //update the post
  59. wp_update_post( array( 'post_content' => 'new update for some updated content', 'ID' => $post_id ) ); //2nd revision
  60. $this->assertCount( 3, wp_get_post_revisions( $post_id ) ); // should be 3 revision so far
  61. //next try to save another identical update, tests for patch that prevents storing duplicates
  62. wp_update_post( array( 'post_content' => 'new update for some updated content', 'ID' => $post_id ) ); //content unchanged, shouldn't save
  63. $this->assertCount( 3, wp_get_post_revisions( $post_id ) ); //should still be 3 revision
  64. //next try to save another update, same content, but new ttile, should save revision
  65. wp_update_post( array( 'post_title' => 'some-post-changed', 'post_content' => 'new update for some updated content', 'ID' => $post_id ) );
  66. $this->assertCount( 4, wp_get_post_revisions( $post_id ) ); //should be 4 revision
  67. //next try to save another identical update
  68. wp_update_post( array( 'post_title' => 'some-post-changed', 'post_content' => 'new update for some updated content', 'ID' => $post_id ) ); //content unchanged, shouldn't save
  69. $this->assertCount( 4, wp_get_post_revisions( $post_id ) ); //should still be 4 revision
  70. }
  71. /**
  72. * @ticket 7392
  73. * @ticket 9843
  74. */
  75. function test_revision_force_save_revision_even_if_unchanged() {
  76. add_filter( 'wp_save_post_revision_check_for_changes', '__return_false' );
  77. $post = get_default_post_to_edit( 'post', true );
  78. $post_id = $post->ID;
  79. $this->assertCount( 0, wp_get_post_revisions( $post_id ) ); // No revisions on auto-draft creation.
  80. wp_update_post( array( 'post_status' => 'draft', 'post_title' => 'some-post', 'post_type' => 'post', 'post_content' => 'some_content', 'ID' => $post_id ) );
  81. $this->assertCount( 1, wp_get_post_revisions( $post_id ) );
  82. wp_update_post( array( 'post_content' => 'some updated content', 'ID' => $post_id ) ); //1st revision
  83. $this->assertCount( 2, wp_get_post_revisions( $post_id ) );
  84. //update the post
  85. wp_update_post( array( 'post_content' => 'new update for some updated content', 'ID' => $post_id ) ); //2nd revision
  86. $this->assertCount( 3, wp_get_post_revisions( $post_id ) );
  87. //next try to save another identical update, tests for patch that prevents storing duplicates
  88. wp_update_post( array( 'post_content' => 'new update for some updated content', 'ID' => $post_id ) ); //content unchanged, shouldn't save
  89. $this->assertCount( 4, wp_get_post_revisions( $post_id ) );
  90. //next try to save another update, same content, but new ttile, should save revision
  91. wp_update_post( array( 'post_title' => 'some-post-changed', 'post_content' => 'new update for some updated content', 'ID' => $post_id ) );
  92. $this->assertCount( 5, wp_get_post_revisions( $post_id ) );
  93. //next try to save another identical update
  94. wp_update_post( array( 'post_title' => 'some-post-changed', 'post_content' => 'new update for some updated content', 'ID' => $post_id ) ); //content unchanged, shouldn't save
  95. $this->assertCount( 6, wp_get_post_revisions( $post_id ) );
  96. remove_filter( 'wp_save_post_revision_check_for_changes', '__return_false' );
  97. }
  98. /**
  99. * Tests the Caps used in the action=view case of wp-admin/revision.php
  100. * @ticket 16847
  101. */
  102. function test_revision_view_caps_post() {
  103. $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) );
  104. $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
  105. $post_id = $this->factory->post->create( array( 'post_type' => 'post', 'post_author' => $editor_user_id ) );
  106. wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
  107. $revisions = wp_get_post_revisions( $post_id );
  108. $this->assertCount( 1, $revisions );
  109. $this->assertTrue( user_can( $editor_user_id, 'read_post', $post_id ) );
  110. foreach ( $revisions as $revision ) {
  111. $this->assertTrue( user_can( $editor_user_id, 'read_post', $revision->ID ) );
  112. }
  113. // Author should be able to view the revisions fine
  114. foreach ( $revisions as $revision ) {
  115. $this->assertTrue( user_can( $author_user_id, 'read_post', $revision->ID ) );
  116. }
  117. }
  118. /**
  119. * Tests the Caps used in the action=restore case of wp-admin/revision.php
  120. * @ticket 16847
  121. */
  122. function test_revision_restore_caps_post() {
  123. $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) );
  124. $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
  125. $post_id = $this->factory->post->create( array( 'post_type' => 'post', 'post_author' => $editor_user_id ) );
  126. wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
  127. $revisions = wp_get_post_revisions( $post_id );
  128. $this->assertCount( 1, $revisions );
  129. foreach ( $revisions as $revision ) {
  130. $this->assertTrue( user_can( $editor_user_id, 'edit_post', $revision->post_parent ) );
  131. }
  132. // Author shouldn't be able to restore the revisions
  133. foreach ( $revisions as $revision ) {
  134. $this->assertFalse( user_can( $author_user_id, 'edit_post', $revision->post_parent ) );
  135. }
  136. }
  137. /**
  138. * Tests the Caps used in the action=diff case of wp-admin/revision.php
  139. * @ticket 16847
  140. */
  141. function test_revision_diff_caps_post() {
  142. $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) );
  143. $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
  144. $post_id = $this->factory->post->create( array( 'post_type' => 'post', 'post_author' => $editor_user_id ) );
  145. wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
  146. wp_update_post( array( 'post_content' => 'This content is even better', 'ID' => $post_id ) );
  147. // Diff checks if you can read both left and right revisions
  148. $revisions = wp_get_post_revisions( $post_id );
  149. $this->assertCount( 2, $revisions );
  150. foreach ( $revisions as $revision ) {
  151. $this->assertTrue( user_can( $editor_user_id, 'read_post', $revision->ID ) );
  152. }
  153. // Author should be able to diff the revisions fine
  154. foreach ( $revisions as $revision ) {
  155. $this->assertTrue( user_can( $author_user_id, 'read_post', $revision->ID ) );
  156. }
  157. }
  158. /**
  159. * Tests the Caps used in the action=view case of wp-admin/revision.php with a CPT with Custom Capabilities
  160. * @ticket 16847
  161. */
  162. function test_revision_view_caps_cpt() {
  163. register_post_type( $this->post_type, array(
  164. 'capability_type' => 'event',
  165. 'map_meta_cap' => true,
  166. 'supports' => array( 'revisions' ),
  167. ) );
  168. $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) );
  169. $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
  170. $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type, 'post_author' => $editor_user_id ) );
  171. wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
  172. $revisions = wp_get_post_revisions( $post_id );
  173. $this->assertCount( 1, $revisions );
  174. $this->assertTrue( user_can( $editor_user_id, 'read_post', $post_id ) );
  175. foreach ( $revisions as $revision ) {
  176. $this->assertTrue( user_can( $editor_user_id, 'read_post', $revision->ID ) );
  177. }
  178. // Author should be able to view the revisions fine
  179. foreach ( $revisions as $revision ) {
  180. $this->assertTrue( user_can( $author_user_id, 'read_post', $revision->ID ) );
  181. }
  182. }
  183. /**
  184. * Tests the Caps used in the action=restore case of wp-admin/revision.php
  185. * @ticket 16847
  186. */
  187. function test_revision_restore_caps_cpt() {
  188. register_post_type( $this->post_type, array(
  189. 'capability_type' => 'event',
  190. 'map_meta_cap' => true,
  191. 'supports' => array( 'revisions' ),
  192. ) );
  193. $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) );
  194. $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
  195. // The minimum extra caps needed for this test normally you would give the role all the relevant caps.
  196. $editor_user = new WP_User( $editor_user_id );
  197. $editor_user->add_cap( 'edit_published_events' );
  198. //create a post as Editor
  199. $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type, 'post_author' => $editor_user_id ) );
  200. wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
  201. $revisions = wp_get_post_revisions( $post_id );
  202. $this->assertCount( 1, $revisions );
  203. foreach ( $revisions as $revision ) {
  204. $this->assertTrue( user_can( $editor_user_id, 'edit_post', $revision->post_parent ) );
  205. }
  206. // Author shouldn't be able to restore the revisions
  207. wp_set_current_user( $author_user_id );
  208. foreach ( $revisions as $revision ) {
  209. $this->assertFalse( user_can( $author_user_id, 'edit_post', $revision->post_parent ) );
  210. }
  211. }
  212. /**
  213. * Tests the Caps used in the action=restore case of wp-admin/revision.php
  214. * @ticket 16847
  215. */
  216. function test_revision_restore_caps_before_publish() {
  217. register_post_type( $this->post_type, array(
  218. 'capability_type' => 'post',
  219. 'capabilities' => array(
  220. // No one can edit this post type once published.
  221. // So, revisions cannot be restored, either.
  222. 'edit_published_posts' => 'do_not_allow',
  223. ),
  224. 'map_meta_cap' => true,
  225. 'supports' => array( 'revisions' ),
  226. ) );
  227. $old_id = get_current_user_id();
  228. wp_set_current_user( $this->factory->user->create( array( 'role' => 'editor' ) ) );
  229. $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type, 'post_status' => 'draft' ) );
  230. wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
  231. $revisions = wp_get_post_revisions( $post_id );
  232. $this->assertCount( 1, $revisions );
  233. foreach ( $revisions as $revision ) {
  234. $this->assertTrue( current_user_can( 'edit_post', $revision->post_parent ) );
  235. $this->assertTrue( current_user_can( 'edit_post', $revision->ID ) );
  236. }
  237. wp_update_post( array( 'post_status' => 'publish', 'ID' => $post_id, 'post_content' => rand_str() ) );
  238. $revisions = wp_get_post_revisions( $post_id );
  239. $this->assertCount( 2, $revisions );
  240. foreach ( $revisions as $revision ) {
  241. $this->assertFalse( current_user_can( 'edit_post', $revision->post_parent ) );
  242. $this->assertFalse( current_user_can( 'edit_post', $revision->ID ) );
  243. }
  244. wp_set_current_user( $old_id );
  245. }
  246. /**
  247. * Tests the Caps used in the action=diff case of wp-admin/revision.php
  248. * @ticket 16847
  249. */
  250. function test_revision_diff_caps_cpt() {
  251. register_post_type( $this->post_type, array(
  252. 'capability_type' => 'event',
  253. 'map_meta_cap' => true,
  254. 'supports' => array( 'revisions' ),
  255. ) );
  256. $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) );
  257. $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
  258. $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type, 'post_author' => $editor_user_id ) );
  259. wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
  260. wp_update_post( array( 'post_content' => 'This content is even better', 'ID' => $post_id ) );
  261. // Diff checks if you can read both left and right revisions
  262. $revisions = wp_get_post_revisions( $post_id );
  263. $this->assertCount( 2, $revisions );
  264. foreach ( $revisions as $revision ) {
  265. $this->assertTrue( user_can( $editor_user_id, 'read_post', $revision->ID ) );
  266. }
  267. // Author should be able to diff the revisions fine
  268. foreach ( $revisions as $revision ) {
  269. $this->assertTrue( user_can( $author_user_id, 'read_post', $revision->ID ) );
  270. }
  271. }
  272. }