Skip to content

Commit ef5c47b

Browse files
committed
Add preview tests
1 parent ca5191f commit ef5c47b

2 files changed

Lines changed: 126 additions & 14 deletions

File tree

tests/controller/post_controller_test.php

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ public function test_controller_exception($forum, $user_id, $status_code, $page_
9595
}
9696

9797
/**
98-
* Test data for the test_submit_success test
98+
* Test data for the test_post_success test
9999
*
100100
* @return array Array of test data
101101
*/
102-
public function submit_success_data()
102+
public function post_success_data()
103103
{
104104
return array(
105105
array(true),
@@ -108,27 +108,27 @@ public function submit_success_data()
108108
}
109109

110110
/**
111-
* Test submit
111+
* Test post
112112
*
113-
* @dataProvider submit_success_data
113+
* @dataProvider post_success_data
114114
*/
115-
public function test_submit_success($is_newly_registered_user)
115+
public function test_post_success($is_newly_registered_user)
116116
{
117117
/** @var \phpbb\ideas\controller\post_controller $controller */
118118
$controller = $this->get_controller('post_controller');
119119
$this->assertInstanceOf('phpbb\ideas\controller\post_controller', $controller);
120120

121-
$this->controller_helper->expects($this->any())
121+
$this->controller_helper->expects($this->once())
122122
->method('route')
123123
->will($this->returnValue('phpbb_ideas_idea_controller'));
124124

125-
$this->request->expects($this->any())
126-
->method('variable')
125+
$this->request->expects($this->once())
126+
->method('is_set_post')
127127
->will($this->returnValueMap(array(
128-
array('mode', '', false, \phpbb\request\request_interface::REQUEST, 'submit'),
128+
array('post', true),
129129
)));
130130

131-
$this->auth->expects($this->any())
131+
$this->auth->expects($this->once())
132132
->method('acl_get')
133133
->with('f_noapprove', $this->config['ideas_forum_id'])
134134
->will($this->returnValue(!$is_newly_registered_user));
@@ -139,14 +139,55 @@ public function test_submit_success($is_newly_registered_user)
139139
}
140140

141141
// ideas->submit() will return an idea id on successful submit
142-
$this->ideas->expects($this->any())
142+
$this->ideas->expects($this->once())
143143
->method('submit')
144144
->will($this->returnValue(1));
145145

146146
$response = $controller->post();
147147
$this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response);
148148
}
149149

150+
/**
151+
* Test preview
152+
*/
153+
public function test_preview()
154+
{
155+
/** @var \phpbb\ideas\controller\post_controller $controller */
156+
$controller = $this->get_controller('post_controller');
157+
$this->assertInstanceOf('phpbb\ideas\controller\post_controller', $controller);
158+
159+
$this->request->expects($this->any())
160+
->method('is_set_post')
161+
->will($this->returnValueMap(array(
162+
array('post', false),
163+
array('preview', true),
164+
)));
165+
166+
$this->request->expects($this->atLeastOnce())
167+
->method('variable')
168+
->will($this->returnValueMap(array(
169+
array('title', '', true, \phpbb\request\request_interface::REQUEST, 'test title'),
170+
array('message', '', true, \phpbb\request\request_interface::REQUEST, 'test message'),
171+
)));
172+
173+
$this->ideas->expects($this->never())
174+
->method('submit');
175+
176+
$this->ideas->expects($this->once())
177+
->method('preview')
178+
->willReturn('test message');
179+
180+
$this->template->expects($this->at(0))
181+
->method('assign_vars')
182+
->with(array(
183+
'S_DISPLAY_PREVIEW' => true,
184+
'PREVIEW_SUBJECT' => 'test title',
185+
'PREVIEW_MESSAGE' => 'test message'
186+
));
187+
188+
$controller->post();
189+
}
190+
150191
/**
151192
* Test submit errors
152193
*/
@@ -156,16 +197,22 @@ public function test_submit_errors()
156197
$controller = $this->get_controller('post_controller');
157198
$this->assertInstanceOf('phpbb\ideas\controller\post_controller', $controller);
158199

159-
$this->request->expects($this->any())
200+
$this->request->expects($this->atLeastOnce())
201+
->method('is_set_post')
202+
->will($this->returnValueMap(array(
203+
array('post', true),
204+
array('preview', false),
205+
)));
206+
207+
$this->request->expects($this->atLeastOnce())
160208
->method('variable')
161209
->will($this->returnValueMap(array(
162-
array('mode', '', false, \phpbb\request\request_interface::REQUEST, 'submit'),
163210
array('title', '', true, \phpbb\request\request_interface::REQUEST, 'test title'),
164211
array('message', '', true, \phpbb\request\request_interface::REQUEST, 'test message'),
165212
)));
166213

167214
// ideas->submit() will return an array of error messages on submit error
168-
$this->ideas->expects($this->any())
215+
$this->ideas->expects($this->once())
169216
->method('submit')
170217
->will($this->returnValue(array('error1', 'error2')));
171218

tests/ideas/preview_idea_test.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
*
4+
* Ideas extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
namespace phpbb\ideas\factory;
12+
13+
class preview_idea_test extends \phpbb\ideas\tests\ideas\ideas_base
14+
{
15+
public function setUp()
16+
{
17+
parent::setUp();
18+
19+
global $phpbb_container;
20+
21+
// This is needed to set up the s9e text formatter services
22+
// This can lead to a test failure if PCRE is old.
23+
$this->get_test_case_helpers()->set_s9e_services($phpbb_container);
24+
}
25+
26+
/**
27+
* Test preview() data
28+
*
29+
* @return array
30+
*/
31+
public function preview_test_data()
32+
{
33+
return array(
34+
array('New idea 1 posted by the test framework.'),
35+
array('New idea 2 [u]posted[/u] by the test [b]framework[/b].'),
36+
);
37+
}
38+
39+
/**
40+
* Test preview()
41+
*
42+
* @dataProvider preview_test_data
43+
*/
44+
public function test_preview($message)
45+
{
46+
// Get the ideas object
47+
$ideas = $this->get_ideas_object();
48+
49+
// store the message in a temp variable, since it will forever altered
50+
$test_message = $message;
51+
52+
// Prepare the test message for storage
53+
$uid = $bitfield = $flags = '';
54+
generate_text_for_storage($test_message, $uid, $bitfield, $flags, true, true, true);
55+
56+
// Prepare the test message for display
57+
$expected = generate_text_for_display($test_message, $uid, $bitfield, $flags);
58+
59+
// Actually run the original test message through preview method
60+
$preview = $ideas->preview($message);
61+
62+
// Assert preview message was parsed and rendered as expected
63+
$this->assertEquals($expected, $preview);
64+
}
65+
}

0 commit comments

Comments
 (0)