Skip to content

Commit 4ad9bd9

Browse files
author
vishalkakadiya
committed
Adding test cases
1 parent d17e6f2 commit 4ad9bd9

File tree

5 files changed

+210
-12
lines changed

5 files changed

+210
-12
lines changed

inc/classes/class-comment-preview.php

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
/**
33
* Class to manage functions for comment preview.
4+
*
5+
* @package WP_Comment_Preview
46
*/
57

68
namespace CommentPreview\Inc;
@@ -17,13 +19,13 @@ class Comment_Preview {
1719
*/
1820
public function __construct() {
1921

20-
$this->_setup_hooks();
22+
$this->setup_hooks();
2123
}
2224

2325
/**
2426
* Initialize actions and filters.
2527
*/
26-
protected function _setup_hooks() {
28+
public function setup_hooks() {
2729

2830
add_filter( 'comment_form_submit_button', array( $this, 'append_preview_button' ) );
2931

@@ -118,7 +120,7 @@ public function append_markdown_option( $field ) {
118120
* @param string $submit_button HTML to output for the submit button.
119121
* @return string Modified HTML
120122
*/
121-
public function append_preview_button( string $submit_button = '' ) {
123+
public function append_preview_button( $submit_button ) {
122124

123125
$preview_button = '<input name="preview" type="button" id="preview" class="submit" value="Preview">';
124126

@@ -152,20 +154,14 @@ public function generate_preview( $request ) {
152154

153155
$response = array();
154156

155-
$response['author'] = 'Anonymous';
156-
157-
if ( ! empty( $request['author'] ) && ! isset( $request['anonymous'] ) ) {
158-
$response['author'] = esc_html( $request['author'] );
159-
}
157+
$user_id = is_user_logged_in() ? get_current_user_id() : 0;
160158

161-
$user_id = ( 'Anonymous' === $response['author'] ) ? 0 : get_current_user_id();
159+
$response['author'] = $user_id;
162160

163161
$response['gravatar'] = get_avatar_url( $user_id, array( 'size' => 50 ) );
164162

165163
$response['date'] = current_time( get_option( 'date_format' ) . ' \a\t ' . get_option( 'time_format' ) );
166164

167-
$response['subject'] = ( isset( $request['subject'] ) ) ? esc_html( $request['subject'] ) : '';
168-
169165
if ( isset( $request['comment'] ) && isset( $request['format'] ) ) {
170166
if ( 'text' === $request['format'] ) {
171167
$comment = wp_kses_data( $request['comment'] );

templates/comment-preview.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
/**
33
* Template for Previewing Comment.
4+
*
5+
* @package WP_Comment_Preview
46
*/
57

68
?>

tests/bootstrap.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* PHPUnit bootstrap file
4+
*
5+
* @package Plugin_Security_Scanner
6+
*/
7+
8+
$_tests_dir = getenv( 'WP_TESTS_DIR' );
9+
if ( ! $_tests_dir ) {
10+
$_tests_dir = '/tmp/wordpress-tests-lib';
11+
}
12+
13+
// Give access to tests_add_filter() function.
14+
require_once $_tests_dir . '/includes/functions.php';
15+
require_once dirname( dirname( __FILE__ ) ) . '/vendor/autoload.php';
16+
17+
/**
18+
* Manually load the plugin being tested.
19+
*/
20+
function _manually_load_plugin() {
21+
//TODO: replace <plugin-sample> with your plugin name
22+
require dirname( dirname( __FILE__ ) ) . '/src/wp-comment-preview.php';
23+
}
24+
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
25+
26+
tests_add_filter('wp_die_handler', function () {
27+
exit(1);
28+
});
29+
30+
// Start up the WP testing environment.
31+
require $_tests_dir . '/includes/bootstrap.php';

tests/test-comment-preview.php

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
}

wp-comment-preview.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Domain Path: /languages
1010
* Version: 0.1.0
1111
*
12-
* @package Comment_Preview
12+
* @package WP_Comment_Preview
1313
*/
1414

1515
if ( ! defined( 'ABSPATH' ) ) {

0 commit comments

Comments
 (0)