|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test class for Comment Preview. |
| 4 | + * |
| 5 | + * @package Plugin_Sample |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Sample test case. |
| 10 | + */ |
| 11 | +class Test_Comment_Preview extends WP_UnitTestCase { |
| 12 | + |
| 13 | + /** |
| 14 | + * Holds the WP REST Server object |
| 15 | + * |
| 16 | + * @var WP_REST_Server |
| 17 | + */ |
| 18 | + private $server; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var |
| 22 | + */ |
| 23 | + protected $instance; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + public $rest_route = '/wp_comment_preview/v1/preview'; |
| 29 | + |
| 30 | + public function setUp() { |
| 31 | + |
| 32 | + parent::setUp(); |
| 33 | + |
| 34 | + // Initiating the REST API. |
| 35 | + global $wp_rest_server; |
| 36 | + |
| 37 | + $this->server = $wp_rest_server = new WP_REST_Server; |
| 38 | + |
| 39 | + do_action( 'rest_api_init' ); |
| 40 | + |
| 41 | + $this->instance = new \CommentPreview\Inc\Comment_Preview(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @covers ::_setup_hooks |
| 46 | + * @covers ::__construct |
| 47 | + */ |
| 48 | + public function test_setup_hooks() { |
| 49 | + |
| 50 | + $this->instance->setup_hooks(); |
| 51 | + |
| 52 | + $hooks = array( |
| 53 | + array( |
| 54 | + 'type' => 'filter', |
| 55 | + 'name' => 'comment_form_submit_button', |
| 56 | + 'priority' => 10, |
| 57 | + 'listener' => 'append_preview_button', |
| 58 | + ), |
| 59 | + array( |
| 60 | + 'type' => 'filter', |
| 61 | + 'name' => 'comment_form_field_comment', |
| 62 | + 'priority' => 20, |
| 63 | + 'listener' => 'append_markdown_option', |
| 64 | + ), |
| 65 | + array( |
| 66 | + 'type' => 'filter', |
| 67 | + 'name' => 'comment_form_fields', |
| 68 | + 'priority' => 20, |
| 69 | + 'listener' => 'comment_form_fields', |
| 70 | + ), |
| 71 | + array( |
| 72 | + 'type' => 'action', |
| 73 | + 'name' => 'wp_enqueue_scripts', |
| 74 | + 'priority' => 10, |
| 75 | + 'listener' => 'enqueue_scripts', |
| 76 | + ), |
| 77 | + array( |
| 78 | + 'type' => 'action', |
| 79 | + 'name' => 'rest_api_init', |
| 80 | + 'priority' => 10, |
| 81 | + 'listener' => 'register_rest_route', |
| 82 | + ), |
| 83 | + ); |
| 84 | + |
| 85 | + foreach ( $hooks as $hook ) { |
| 86 | + |
| 87 | + $this->assertEquals( |
| 88 | + $hook['priority'], |
| 89 | + call_user_func( sprintf( 'has_%s', $hook['type'] ), $hook['name'], array( $this->instance, $hook['listener'] ) ), |
| 90 | + sprintf( '\Deadline\Inc\Rest_Api::_setup_hooks() failed to register %1$s "%2$s" to %3$s()', $hook['type'], $hook['name'], $hook['listener'] ) |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @covers ::register_rest_route |
| 97 | + */ |
| 98 | + public function test_register_rest_route() { |
| 99 | + |
| 100 | + $this->instance->setup_hooks(); |
| 101 | + |
| 102 | + do_action( 'rest_api_init' ); |
| 103 | + |
| 104 | + $routes = $this->server->get_routes(); |
| 105 | + |
| 106 | + $custom_route = '/wp_comment_preview/v1/preview'; |
| 107 | + |
| 108 | + $this->assertArrayHasKey( $custom_route, $routes ); |
| 109 | + |
| 110 | + $endpoint = $routes[ $custom_route ][0]; |
| 111 | + |
| 112 | + $this->assertTrue( is_callable( $endpoint['callback'] ) ); |
| 113 | + $this->assertEquals( [ $this->instance, 'generate_preview' ], $endpoint['callback'] ); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @covers ::generate_preview |
| 118 | + */ |
| 119 | + public function test_generate_preview() { |
| 120 | + |
| 121 | + /** |
| 122 | + * Test as Annoymous user. |
| 123 | + */ |
| 124 | + $request = new WP_REST_Request( 'POST', $this->rest_route ); |
| 125 | + |
| 126 | + $request->set_param( 'comment', '[hello](https://www.google.com)' ); |
| 127 | + $request->set_param( 'format', 'markdown' ); |
| 128 | + |
| 129 | + $response = $this->server->dispatch( $request ); |
| 130 | + |
| 131 | + $this->assertEquals( 200, $response->status ); |
| 132 | + |
| 133 | + $this->assertEquals( '<a href="https://www.google.com">hello</a>', $response->data['comment'] ); |
| 134 | + |
| 135 | + /** |
| 136 | + * Testing as login user. |
| 137 | + */ |
| 138 | + $user_id = $this->factory->user->create( [ 'role' => 'administrator' ] ); |
| 139 | + |
| 140 | + wp_set_current_user( $user_id ); |
| 141 | + |
| 142 | + $request = new WP_REST_Request( 'POST', $this->rest_route ); |
| 143 | + |
| 144 | + $request->set_param( 'comment', '## h2 Heading' ); |
| 145 | + $request->set_param( 'format', 'markdown' ); |
| 146 | + |
| 147 | + $response = $this->server->dispatch( $request ); |
| 148 | + |
| 149 | + $this->assertEquals( 200, $response->status ); |
| 150 | + |
| 151 | + $this->assertEquals( $user_id, $response->data['author'] ); |
| 152 | + |
| 153 | + $avatar_url = get_avatar_url( $user_id, array( 'size' => 50 ) ); |
| 154 | + |
| 155 | + $this->assertEquals( $avatar_url, $response->data['gravatar'] ); |
| 156 | + |
| 157 | + $this->assertEquals( '<h2>h2 Heading</h2>', $response->data['comment'] ); |
| 158 | + } |
| 159 | + |
| 160 | + public function tearDown() { |
| 161 | + |
| 162 | + parent::tearDown(); |
| 163 | + |
| 164 | + global $wp_rest_server; |
| 165 | + |
| 166 | + $wp_rest_server = null; |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments