|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the collaboration autosaves REST controller override. |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + * @subpackage Collaboration |
| 7 | + * |
| 8 | + * @group collaboration |
| 9 | + * @group restapi |
| 10 | + */ |
| 11 | +class Tests_Collaboration_RestAutosavesController extends WP_UnitTestCase { |
| 12 | + |
| 13 | + protected static int $author_id; |
| 14 | + protected static int $editor_id; |
| 15 | + |
| 16 | + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| 17 | + self::$author_id = $factory->user->create( array( 'role' => 'author' ) ); |
| 18 | + self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) ); |
| 19 | + } |
| 20 | + |
| 21 | + public static function wpTearDownAfterClass() { |
| 22 | + self::delete_user( self::$author_id ); |
| 23 | + self::delete_user( self::$editor_id ); |
| 24 | + delete_option( 'wp_collaboration_enabled' ); |
| 25 | + } |
| 26 | + |
| 27 | + public function set_up() { |
| 28 | + parent::set_up(); |
| 29 | + wp_set_current_user( self::$author_id ); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Creates an empty auto-draft post. |
| 34 | + * |
| 35 | + * @return int Post ID. |
| 36 | + */ |
| 37 | + private function create_auto_draft(): int { |
| 38 | + return self::factory()->post->create( |
| 39 | + array( |
| 40 | + 'post_author' => self::$author_id, |
| 41 | + 'post_content' => '', |
| 42 | + 'post_status' => 'auto-draft', |
| 43 | + 'post_title' => 'Auto Draft', |
| 44 | + 'post_type' => 'post', |
| 45 | + ) |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Creates a draft post. |
| 51 | + * |
| 52 | + * @param string $title Post title. |
| 53 | + * @param string $content Post content. |
| 54 | + * @return int Post ID. |
| 55 | + */ |
| 56 | + private function create_draft( string $title, string $content ): int { |
| 57 | + return self::factory()->post->create( |
| 58 | + array( |
| 59 | + 'post_author' => self::$author_id, |
| 60 | + 'post_content' => $content, |
| 61 | + 'post_status' => 'draft', |
| 62 | + 'post_title' => $title, |
| 63 | + 'post_type' => 'post', |
| 64 | + ) |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Dispatches an autosave request for a post. |
| 70 | + * |
| 71 | + * @param int $post_id Post ID. |
| 72 | + * @param string $title Autosaved post title. |
| 73 | + * @param string $content Autosaved post content. |
| 74 | + * @return WP_REST_Response Autosave response. |
| 75 | + */ |
| 76 | + private function dispatch_autosave( int $post_id, string $title, string $content ): WP_REST_Response { |
| 77 | + $request = new WP_REST_Request( 'POST', "/wp/v2/posts/{$post_id}/autosaves" ); |
| 78 | + $request->set_param( 'title', $title ); |
| 79 | + $request->set_param( 'content', $content ); |
| 80 | + $request->set_param( 'status', 'draft' ); |
| 81 | + |
| 82 | + return rest_get_server()->dispatch( $request ); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @ticket 65138 |
| 87 | + */ |
| 88 | + public function test_auto_draft_autosave_promotes_parent_post_when_collaboration_is_disabled() { |
| 89 | + update_option( 'wp_collaboration_enabled', 0 ); |
| 90 | + |
| 91 | + $post_id = $this->create_auto_draft(); |
| 92 | + $title = 'No RTC autosaved title'; |
| 93 | + $content = '<!-- wp:paragraph --><p>No RTC autosaved content</p><!-- /wp:paragraph -->'; |
| 94 | + |
| 95 | + $response = $this->dispatch_autosave( $post_id, $title, $content ); |
| 96 | + |
| 97 | + $this->assertSame( 200, $response->get_status() ); |
| 98 | + $post = get_post( $post_id ); |
| 99 | + $this->assertSame( 'draft', $post->post_status ); |
| 100 | + $this->assertSame( $title, $post->post_title ); |
| 101 | + $this->assertSame( $content, $post->post_content ); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @ticket 65138 |
| 106 | + */ |
| 107 | + public function test_auto_draft_autosave_promotes_parent_post_when_collaboration_is_enabled() { |
| 108 | + update_option( 'wp_collaboration_enabled', 1 ); |
| 109 | + |
| 110 | + $post_id = $this->create_auto_draft(); |
| 111 | + $title = 'RTC autosaved title'; |
| 112 | + $content = '<!-- wp:paragraph --><p>RTC autosaved content</p><!-- /wp:paragraph -->'; |
| 113 | + |
| 114 | + $response = $this->dispatch_autosave( $post_id, $title, $content ); |
| 115 | + |
| 116 | + $this->assertSame( 200, $response->get_status() ); |
| 117 | + $post = get_post( $post_id ); |
| 118 | + $this->assertSame( 'draft', $post->post_status ); |
| 119 | + $this->assertSame( $title, $post->post_title ); |
| 120 | + $this->assertSame( $content, $post->post_content ); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @ticket 65138 |
| 125 | + */ |
| 126 | + public function test_collaborator_auto_draft_autosave_promotes_parent_post_when_collaboration_is_enabled() { |
| 127 | + update_option( 'wp_collaboration_enabled', 1 ); |
| 128 | + |
| 129 | + $post_id = $this->create_auto_draft(); |
| 130 | + $title = 'RTC collaborator autosaved title'; |
| 131 | + $content = '<!-- wp:paragraph --><p>RTC collaborator autosaved content</p><!-- /wp:paragraph -->'; |
| 132 | + |
| 133 | + wp_set_current_user( self::$editor_id ); |
| 134 | + $response = $this->dispatch_autosave( $post_id, $title, $content ); |
| 135 | + |
| 136 | + $this->assertSame( 200, $response->get_status() ); |
| 137 | + $post = get_post( $post_id ); |
| 138 | + $this->assertSame( 'draft', $post->post_status ); |
| 139 | + $this->assertSame( $title, $post->post_title ); |
| 140 | + $this->assertSame( $content, $post->post_content ); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @ticket 65138 |
| 145 | + */ |
| 146 | + public function test_draft_autosave_creates_revision_when_collaboration_is_enabled() { |
| 147 | + update_option( 'wp_collaboration_enabled', 1 ); |
| 148 | + |
| 149 | + $original_title = 'Original RTC draft title'; |
| 150 | + $original_content = '<!-- wp:paragraph --><p>Original RTC draft content</p><!-- /wp:paragraph -->'; |
| 151 | + $post_id = $this->create_draft( $original_title, $original_content ); |
| 152 | + $title = 'RTC draft autosaved title'; |
| 153 | + $content = '<!-- wp:paragraph --><p>RTC draft autosaved content</p><!-- /wp:paragraph -->'; |
| 154 | + |
| 155 | + $response = $this->dispatch_autosave( $post_id, $title, $content ); |
| 156 | + |
| 157 | + $this->assertSame( 200, $response->get_status() ); |
| 158 | + $post = get_post( $post_id ); |
| 159 | + $this->assertSame( 'draft', $post->post_status ); |
| 160 | + $this->assertSame( $original_title, $post->post_title ); |
| 161 | + $this->assertSame( $original_content, $post->post_content ); |
| 162 | + |
| 163 | + $autosave = wp_get_post_autosave( $post_id, self::$author_id ); |
| 164 | + $this->assertInstanceOf( WP_Post::class, $autosave ); |
| 165 | + $this->assertSame( $title, $autosave->post_title ); |
| 166 | + $this->assertSame( $content, $autosave->post_content ); |
| 167 | + } |
| 168 | +} |
0 commit comments