Skip to content

Commit a15048c

Browse files
authored
Merge pull request #106 from VSEphpbb/preview
Add post preview capability
2 parents 91efbaf + ef5c47b commit a15048c

5 files changed

Lines changed: 154 additions & 17 deletions

File tree

controller/post_controller.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@ public function post()
4545
include $this->root_path . 'includes/functions_display.' . $this->php_ext;
4646
}
4747

48-
$mode = $this->request->variable('mode', '');
4948
$title = $this->request->variable('title', '', true);
5049
$message = $this->request->variable('message', '', true);
5150

52-
if ($mode === 'submit')
51+
if ($this->request->is_set_post('post'))
5352
{
5453
$submit = $this->ideas->submit($title, $message, $this->user->data['user_id']);
5554

@@ -71,6 +70,17 @@ public function post()
7170
}
7271
}
7372

73+
if ($this->request->is_set_post('preview'))
74+
{
75+
$preview_message = $this->ideas->preview($message);
76+
77+
$this->template->assign_vars(array(
78+
'S_DISPLAY_PREVIEW' => !empty($preview_message),
79+
'PREVIEW_SUBJECT' => $title,
80+
'PREVIEW_MESSAGE' => $preview_message,
81+
));
82+
}
83+
7484
display_custom_bbcodes();
7585
generate_smilies('inline', 0);
7686

@@ -83,8 +93,9 @@ public function post()
8393

8494
$this->template->assign_vars(array(
8595
'TITLE' => $title,
96+
'MESSAGE' => $message,
8697

87-
'S_POST_ACTION' => $this->helper->route('phpbb_ideas_post_controller', array('mode' => 'submit')),
98+
'S_POST_ACTION' => $this->helper->route('phpbb_ideas_post_controller'),
8899
'S_BBCODE_ALLOWED' => $bbcode_status,
89100
'S_SMILIES_ALLOWED' => $smilies_status,
90101
'S_LINKS_ALLOWED' => $url_status,

factory/ideas.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,19 @@ public function submit($title, $message, $user_id)
670670
return $idea_id;
671671
}
672672

673+
/**
674+
* Preview a new idea.
675+
*
676+
* @param string $message The description of the idea.
677+
* @return string The idea parsed for display in preview.
678+
*/
679+
public function preview($message)
680+
{
681+
$uid = $bitfield = $flags = '';
682+
generate_text_for_storage($message, $uid, $bitfield, $flags, true, true, true);
683+
return generate_text_for_display($message, $uid, $bitfield, $flags);
684+
}
685+
673686
/**
674687
* Deletes an idea and the topic to go with it.
675688
*

styles/prosilver/template/idea_new.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ <h3>{{ lang('POST_IDEA') }}</h3>
6161
<fieldset class="submit-buttons">
6262
{{ S_HIDDEN_FIELDS }}
6363
<input type="submit" accesskey="s" tabindex="6" name="post" value="{{ lang('SUBMIT') }}" class="button1 default-submit-action" />
64+
<input type="submit" name="preview" value="{{ lang('PREVIEW') }}" class="button2" />
6465
</fieldset>
6566
</div>
6667
</div>

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)