-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest-postReply.php
More file actions
executable file
·135 lines (124 loc) · 4.88 KB
/
test-postReply.php
File metadata and controls
executable file
·135 lines (124 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* Class PostTopic
*
* @package Bbp_API
*/
// Give access to common functions for tests.
require_once( 'commonFunctions.php' );
/**
* Testing the posting a topic.
*/
class PostReply extends WP_UnitTestCase {
/**
* setting up the WP REST Server
*/
protected $prefix = "/bbp-api/v1/forums";
protected $registeredRoute = "/reply";
protected $topic_data = array(
"title" => "Test Topic.",
"content" => "Initial Content.",
);
protected $reply_data = array(
"title" => "Test reply.",
"content" => "Second reply in the thread.",
);
protected $user_email = "admin@example.org";
function setUp() {
parent::setUp();
global $wp_rest_server;
$testCommon = new Bbp_API_test_common();
$testCommon->activateBBPress();
$testCommon->activateBBPAPI();
$this->server = $wp_rest_server = new WP_REST_Server;
do_action( 'rest_api_init' );
$this->newForum = $testCommon->createBBPForum();
$this->newTopic = $testCommon->createBBPTopic($this->newForum,
$this->topic_data);
}
/**
* A single example test.
*/
function testPostReply() {
//get initial reply on test thread
$topicRequest = new WP_REST_Request( "GET",
$this->prefix . "/topic/" . $this->newTopic );
$topicResponse = $this->server->dispatch( $topicRequest );
//POST a reply to the returned reply id
$replyRequest = new WP_REST_Request( "POST",
$this->prefix . $this->registeredRoute . "/" . $topicResponse->data["last_reply"] );
$replyRequest->set_body_params( array(
"content" => $this->reply_data["content"],
"email" => $this->user_email,
));
$replyResponse = $this->server->dispatch( $replyRequest );
$this->assertEquals( 200, $replyResponse->status );
//retrieve the POSTed content.
$verifyRequest = new WP_REST_Request( "GET",
$this->prefix . $this->registeredRoute . "/" . $replyResponse->data );
$verifyResponse = $this->server->dispatch( $verifyRequest );
$this->assertEquals( $this->reply_data["content"],
$verifyResponse->data["content"] );
}
function testPostReplyNoContent() {
//get initial reply on test thread
$topicRequest = new WP_REST_Request( "GET",
$this->prefix . "/topic/" . $this->newTopic );
$topicResponse = $this->server->dispatch( $topicRequest );
//POST a reply to the returned reply id
$replyRequest = new WP_REST_Request( "POST",
$this->prefix . $this->registeredRoute . "/" . $topicResponse->data["last_reply"] );
$replyRequest->set_body_params( array(
"email" => $this->user_email,
));
$replyResponse = $this->server->dispatch( $replyRequest );
$this->assertNotEquals( 200, $replyResponse->status );
$this->assertEquals( "rest_missing_callback_param", $replyResponse->data["code"] );
$this->assertContains( "content", $replyResponse->data["data"]["params"] );
}
function testPostReplyNoEmail() {
//get initial reply on test thread
$topicRequest = new WP_REST_Request( "GET",
$this->prefix . "/topic/" . $this->newTopic );
$topicResponse = $this->server->dispatch( $topicRequest );
//POST a reply to the returned reply id
$replyRequest = new WP_REST_Request( "POST",
$this->prefix . $this->registeredRoute . "/" . $topicResponse->data["last_reply"] );
$replyRequest->set_body_params( array(
"content" => $this->reply_data["content"],
));
$replyResponse = $this->server->dispatch( $replyRequest );
$this->assertNotEquals( 200, $replyResponse->status );
$this->assertEquals( "rest_missing_callback_param", $replyResponse->data["code"] );
$this->assertContains( "email", $replyResponse->data["data"]["params"] );
}
function testPostReplyBadInput() {
//get initial reply on test thread
$topicRequest = new WP_REST_Request( "GET",
$this->prefix . "/topic/" . $this->newTopic );
$topicResponse = $this->server->dispatch( $topicRequest );
//POST with bad content
$badContentRequest = new WP_REST_Request( "POST",
$this->prefix . $this->registeredRoute . "/" . $topicResponse->data["last_reply"] );
$badContentRequest->set_body_params( array(
"content" => 1234,
"email" => $this->user_email,
));
$badContentResponse = $this->server->dispatch( $badContentRequest );
$this->assertEquals( 400, $badContentResponse->status );
$this->assertEquals( "rest_invalid_param", $badContentResponse->data["code"] );
//POST with bad email
$badEmailRequest = new WP_REST_Request( "POST",
$this->prefix . $this->registeredRoute . "/" . $topicResponse->data["last_reply"] );
$badEmailRequest->set_body_params( array(
"content" => $this->reply_data["content"],
"email" => 1234,
));
$badEmailResponse = $this->server->dispatch( $badEmailRequest );
$this->assertEquals( 400, $badEmailResponse->status );
$this->assertEquals( "rest_invalid_param", $badEmailResponse->data["code"] );
}
function tearDown() {
parent::tearDown();
}
}