|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @group post |
| 5 | + */ |
| 6 | +class Tests_Post_wpGetPostAutosave extends WP_UnitTestCase { |
| 7 | + |
| 8 | + /** |
| 9 | + * Admin user ID. |
| 10 | + * |
| 11 | + * @var int |
| 12 | + */ |
| 13 | + protected static $admin_id; |
| 14 | + |
| 15 | + /** |
| 16 | + * Editor user ID. |
| 17 | + * |
| 18 | + * @var int |
| 19 | + */ |
| 20 | + protected static $editor_id; |
| 21 | + |
| 22 | + /** |
| 23 | + * Post ID. |
| 24 | + * |
| 25 | + * @var int |
| 26 | + */ |
| 27 | + protected static $post_id; |
| 28 | + |
| 29 | + /** |
| 30 | + * Set up before class. |
| 31 | + * |
| 32 | + * @param WP_UnitTest_Factory $factory Factory. |
| 33 | + */ |
| 34 | + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| 35 | + self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) ); |
| 36 | + self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) ); |
| 37 | + |
| 38 | + wp_set_current_user( self::$admin_id ); |
| 39 | + self::$post_id = $factory->post->create( array( 'post_status' => 'publish' ) ); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Test when no autosave exists for a post. |
| 44 | + * |
| 45 | + * @ticket 62658 |
| 46 | + */ |
| 47 | + public function test_no_autosave_exists() { |
| 48 | + $autosave = wp_get_post_autosave( self::$post_id ); |
| 49 | + $this->assertFalse( $autosave, 'Expected no autosave.' ); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Test when an autosave exists for a post. |
| 54 | + * |
| 55 | + * @ticket 62658 |
| 56 | + */ |
| 57 | + public function test_autosave_exists() { |
| 58 | + $autosave_id = $this->factory()->post->create( |
| 59 | + array( |
| 60 | + 'post_type' => 'revision', |
| 61 | + 'post_status' => 'inherit', |
| 62 | + 'post_parent' => self::$post_id, |
| 63 | + 'post_author' => self::$admin_id, |
| 64 | + 'post_content' => 'Autosaved content', |
| 65 | + 'post_name' => self::$post_id . '-autosave-v1', |
| 66 | + ) |
| 67 | + ); |
| 68 | + |
| 69 | + $autosave = wp_get_post_autosave( self::$post_id ); |
| 70 | + |
| 71 | + $this->assertInstanceOf( 'WP_Post', $autosave ); |
| 72 | + $this->assertSame( $autosave_id, $autosave->ID, 'Autosave ID does not match.' ); |
| 73 | + $this->assertSame( self::$post_id, (int) $autosave->post_parent, 'Post parent ID does not match.' ); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Test when an autosave exists for a specific user. |
| 78 | + * |
| 79 | + * @ticket 62658 |
| 80 | + */ |
| 81 | + public function test_autosave_for_specific_user() { |
| 82 | + $autosave_id = $this->factory()->post->create( |
| 83 | + array( |
| 84 | + 'post_type' => 'revision', |
| 85 | + 'post_status' => 'inherit', |
| 86 | + 'post_parent' => self::$post_id, |
| 87 | + 'post_author' => self::$editor_id, |
| 88 | + 'post_content' => 'Editor-specific autosave', |
| 89 | + 'post_name' => self::$post_id . '-autosave-v1', |
| 90 | + ) |
| 91 | + ); |
| 92 | + |
| 93 | + $autosave = wp_get_post_autosave( self::$post_id, self::$editor_id ); |
| 94 | + |
| 95 | + $this->assertInstanceOf( 'WP_Post', $autosave ); |
| 96 | + $this->assertSame( self::$editor_id, (int) $autosave->post_author, 'Post author does not match.' ); |
| 97 | + $this->assertSame( $autosave_id, $autosave->ID, 'Autosave ID does not match.' ); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Test when an autosave is updated. |
| 102 | + * |
| 103 | + * @ticket 62658 |
| 104 | + */ |
| 105 | + public function test_autosave_exists_update_caches() { |
| 106 | + $autosave_id = $this->factory()->post->create( |
| 107 | + array( |
| 108 | + 'post_type' => 'revision', |
| 109 | + 'post_status' => 'inherit', |
| 110 | + 'post_parent' => self::$post_id, |
| 111 | + 'post_author' => self::$admin_id, |
| 112 | + 'post_content' => 'Autosaved content', |
| 113 | + 'post_name' => self::$post_id . '-autosave-v1', |
| 114 | + ) |
| 115 | + ); |
| 116 | + |
| 117 | + $autosave = wp_get_post_autosave( self::$post_id ); |
| 118 | + |
| 119 | + $this->assertInstanceOf( 'WP_Post', $autosave ); |
| 120 | + $this->assertSame( $autosave_id, $autosave->ID, 'Autosave ID does not match.' ); |
| 121 | + $this->assertSame( self::$post_id, (int) $autosave->post_parent, 'Post parent ID does not match.' ); |
| 122 | + $this->assertSame( 'Autosaved content', $autosave->post_content, 'Post content does not match.' ); |
| 123 | + |
| 124 | + wp_update_post( |
| 125 | + array( |
| 126 | + 'ID' => $autosave->ID, |
| 127 | + 'post_content' => 'Autosaved content updated', |
| 128 | + ) |
| 129 | + ); |
| 130 | + |
| 131 | + $autosave = wp_get_post_autosave( self::$post_id ); |
| 132 | + $this->assertInstanceOf( 'WP_Post', $autosave ); |
| 133 | + $this->assertSame( 'Autosaved content updated', $autosave->post_content, 'Post content does not match.' ); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Test when an autosave is deleted |
| 138 | + * |
| 139 | + * @ticket 62658 |
| 140 | + */ |
| 141 | + public function test_autosave_exists_and_deleted() { |
| 142 | + $autosave_id = $this->factory()->post->create( |
| 143 | + array( |
| 144 | + 'post_type' => 'revision', |
| 145 | + 'post_status' => 'inherit', |
| 146 | + 'post_parent' => self::$post_id, |
| 147 | + 'post_author' => self::$admin_id, |
| 148 | + 'post_content' => 'Autosaved content', |
| 149 | + 'post_name' => self::$post_id . '-autosave-v1', |
| 150 | + ) |
| 151 | + ); |
| 152 | + |
| 153 | + $autosave = wp_get_post_autosave( self::$post_id ); |
| 154 | + |
| 155 | + $this->assertInstanceOf( 'WP_Post', $autosave ); |
| 156 | + $this->assertSame( $autosave_id, $autosave->ID, 'Autosave ID does not match.' ); |
| 157 | + $this->assertSame( self::$post_id, (int) $autosave->post_parent, 'Post parent ID does not match.' ); |
| 158 | + $this->assertSame( 'Autosaved content', $autosave->post_content, 'Post content does not match.' ); |
| 159 | + |
| 160 | + wp_delete_post( $autosave->ID, true ); |
| 161 | + |
| 162 | + $autosave = wp_get_post_autosave( self::$post_id ); |
| 163 | + $this->assertFalse( $autosave, 'Autosave should not exist' ); |
| 164 | + } |
| 165 | +} |
0 commit comments