Skip to content

Commit b1a112c

Browse files
authored
Merge pull request #129 from VSEphpbb/improvements
Improve Live Search feature (and overall test coverage)
2 parents 7f0f9f5 + 04c1041 commit b1a112c

7 files changed

Lines changed: 264 additions & 8 deletions

File tree

controller/livesearch_controller.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@
1515
*/
1616
class livesearch_controller extends base
1717
{
18+
/**
19+
* Title search handler
20+
*
21+
* @return \Symfony\Component\HttpFoundation\JsonResponse
22+
*/
1823
public function title_search()
1924
{
2025
$title_chars = $this->request->variable('duplicateeditinput', '', true);
2126

2227
$matches = $this->ideas->ideas_title_livesearch($title_chars, 10);
2328

24-
$json_response = new \phpbb\json_response();
25-
$json_response->send([
29+
return new \Symfony\Component\HttpFoundation\JsonResponse([
2630
'keyword' => $title_chars,
2731
'results' => $matches,
2832
]);

factory/ideas.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ public function ideas_title_livesearch($search, $limit = 10)
346346
$results = [];
347347
$sql = 'SELECT idea_title, idea_id
348348
FROM ' . $this->table_ideas . '
349-
WHERE idea_title ' . $this->db->sql_like_expression($search . $this->db->get_any_char());
349+
WHERE idea_title ' . $this->db->sql_like_expression($this->db->get_any_char() . $search . $this->db->get_any_char());
350350
$result = $this->db->sql_query_limit($sql, $limit);
351351
while ($row = $this->db->sql_fetchrow($result))
352352
{
@@ -518,7 +518,7 @@ public function set_title($idea_id, $title)
518518
*
519519
* @param int $id ID of an idea
520520
*
521-
* @return string The idea's title
521+
* @return string The idea's title, empty string if not found
522522
*/
523523
public function get_title($id)
524524
{
@@ -529,7 +529,7 @@ public function get_title($id)
529529
$idea_title = $this->db->sql_fetchfield('idea_title');
530530
$this->db->sql_freeresult($result);
531531

532-
return $idea_title;
532+
return $idea_title ?: '';
533533
}
534534

535535
/**
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace phpbb\ideas\migrations;
1212

13-
class m12_set_permissions extends \phpbb\db\migration\migration
13+
class m13_set_permissions extends \phpbb\db\migration\migration
1414
{
1515
/**
1616
* {@inheritDoc}
@@ -20,7 +20,7 @@ public static function depends_on()
2020
return [
2121
'\phpbb\ideas\migrations\m1_initial_schema',
2222
'\phpbb\ideas\migrations\m3_acp_data',
23-
'\phpbb\ideas\migrations\m11_reparse_old_ideas',
23+
'\phpbb\ideas\migrations\m12_drop_base_url_config',
2424
];
2525
}
2626

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\tests\controller;
12+
13+
class livesearch_controller_test extends controller_base
14+
{
15+
/**
16+
* Test data for the test_controller test
17+
*
18+
* @return array Array of test data
19+
*/
20+
public function controller_test_data()
21+
{
22+
return [
23+
[200, ['keyword' => 'foo', 'results' => '$matches']],
24+
[200, ['keyword' => 'bar', 'results' => '']],
25+
];
26+
}
27+
28+
/**
29+
* Live search controller test
30+
*
31+
* @dataProvider controller_test_data
32+
*/
33+
public function test_controller($status_code, $content)
34+
{
35+
$this->request->expects($this->once())
36+
->method('variable')
37+
->with('duplicateeditinput', '', true)
38+
->willReturn($content['keyword']);
39+
40+
$this->ideas->expects($this->once())
41+
->method('ideas_title_livesearch')
42+
->with($content['keyword'], 10)
43+
->willReturn($content['results']);
44+
45+
/** @var \phpbb\ideas\controller\livesearch_controller $controller */
46+
$controller = $this->get_controller('livesearch_controller');
47+
$this->assertInstanceOf('phpbb\ideas\controller\livesearch_controller', $controller);
48+
49+
$response = $controller->title_search();
50+
$this->assertInstanceOf('\Symfony\Component\HttpFoundation\JsonResponse', $response);
51+
$this->assertEquals($status_code, $response->getStatusCode());
52+
$this->assertEquals(json_encode($content), $response->getContent());
53+
}
54+
}

tests/event/listener_test.php

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,68 @@ public function test_show_post_buttons($forum_id, $post_id, $first_post_id, $pos
168168
$this->assertTrue($event['post_row']['U_INFO']);
169169
}
170170

171+
/**
172+
* Data set for adjust_quickmod_tools
173+
*
174+
* @return array Array of test data
175+
*/
176+
public function adjust_quickmod_tools_data()
177+
{
178+
$quickmod_array = [
179+
'lock' => [1 => true],
180+
'unlock' => [1 => true],
181+
'delete_topic' => [1 => true],
182+
'restore_topic' => [1 => true],
183+
'move' => [1 => true],
184+
'split' => [1 => true],
185+
'merge' => [1 => true],
186+
'merge_topic' => [1 => true],
187+
'fork' => [1 => true],
188+
'make_normal' => [1 => true],
189+
'make_sticky' => [1 => true],
190+
'make_announce' => [1 => true],
191+
'make_global' => [1 => true],
192+
];
193+
194+
return [
195+
[2, $quickmod_array, false], // Valid
196+
[1, $quickmod_array, true], // Invalid forum
197+
];
198+
}
199+
200+
/**
201+
* Test the adjust_quickmod_tools event
202+
*
203+
* @dataProvider adjust_quickmod_tools_data
204+
*/
205+
public function test_adjust_quickmod_tools($forum_id, $quickmod_array, $expected)
206+
{
207+
$listener = $this->get_listener();
208+
209+
$event = new \phpbb\event\data([
210+
'forum_id' => $forum_id,
211+
'quickmod_array' => $quickmod_array,
212+
]);
213+
214+
$listener->adjust_quickmod_tools($event);
215+
216+
$this->assertEquals($expected, $event['quickmod_array']['delete_topic'][1]);
217+
$this->assertEquals($expected, $event['quickmod_array']['restore_topic'][1]);
218+
$this->assertEquals($expected, $event['quickmod_array']['make_normal'][1]);
219+
$this->assertEquals($expected, $event['quickmod_array']['make_sticky'][1]);
220+
$this->assertEquals($expected, $event['quickmod_array']['make_announce'][1]);
221+
$this->assertEquals($expected, $event['quickmod_array']['make_global'][1]);
222+
223+
// These should always be true since we're not changing them
224+
$this->assertTrue($event['quickmod_array']['lock'][1]);
225+
$this->assertTrue($event['quickmod_array']['unlock'][1]);
226+
$this->assertTrue($event['quickmod_array']['move'][1]);
227+
$this->assertTrue($event['quickmod_array']['split'][1]);
228+
$this->assertTrue($event['quickmod_array']['merge'][1]);
229+
$this->assertTrue($event['quickmod_array']['merge_topic'][1]);
230+
$this->assertTrue($event['quickmod_array']['fork'][1]);
231+
}
232+
171233
/**
172234
* Data set for test_viewonline
173235
*
@@ -381,6 +443,41 @@ public function test_edit_idea_title($data, $expected)
381443
$listener->edit_idea_title($event);
382444
}
383445

446+
/**
447+
* Test data for test_ideas_forum_redirect
448+
*/
449+
public function ideas_forum_redirect_data()
450+
{
451+
return [
452+
[2, '$url', true],
453+
[4, '$url', false],
454+
];
455+
}
456+
457+
/**
458+
* Test the ideas_forum_redirect() method
459+
*
460+
* @dataProvider ideas_forum_redirect_data
461+
*/
462+
public function test_ideas_forum_redirect($forum_id, $url, $expected)
463+
{
464+
if ($expected)
465+
{
466+
$this->setExpectedTriggerError(E_USER_NOTICE, "Redirected to $url");
467+
}
468+
$this->helper->expects($expected ? $this->once() : $this->never())
469+
->method('route')
470+
->willReturn($url);
471+
472+
$listener = $this->get_listener();
473+
474+
$event = new \phpbb\event\data([
475+
'forum_id' => $forum_id,
476+
]);
477+
478+
$listener->ideas_forum_redirect($event);
479+
}
480+
384481
public function submit_idea_data()
385482
{
386483
return [
@@ -475,9 +572,20 @@ public function test_submit_idea($mode, $forum_id, $topic_id, $approved, $succes
475572
}
476573
}
477574

575+
/**
576+
* Mock redirect()
577+
* Note: use the same namespace as the ideas
578+
*
579+
* @return void
580+
*/
581+
function redirect($url)
582+
{
583+
trigger_error("Redirected to $url", E_USER_NOTICE);
584+
}
585+
478586
/**
479587
* Mock meta_refresh()
480-
* Note: use the same namespace as the idea_controller
588+
* Note: use the same namespace as the ideas
481589
*
482590
* @return void
483591
*/

tests/ideas/get_title_test.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\tests\ideas;
12+
13+
class get_title_test extends \phpbb\ideas\tests\ideas\ideas_base
14+
{
15+
public function get_title_data()
16+
{
17+
return [
18+
[1, 'Foo Idea #1 New with 3 up votes'],
19+
[2, 'Bar Idea #2 New with 1 down vote'],
20+
[3, 'Foo Idea #3 In Progress with 1 up and 1 down vote'],
21+
[4, 'Bar Idea #4 Implemented with 0 votes'],
22+
[5, 'Unapproved Idea #5 New with 0 votes'],
23+
[6, 'Orphaned Idea #6 New with 2 votes'],
24+
[7, 'Orphaned Idea #7 New with no votes'],
25+
[8, ''],
26+
];
27+
}
28+
29+
/**
30+
* @dataProvider get_title_data
31+
*/
32+
public function test_get_title($id, $expected)
33+
{
34+
$ideas = $this->get_ideas_object();
35+
36+
$this->assertSame($expected, $ideas->get_title($id));
37+
}
38+
}

tests/ideas/live_search_test.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\tests\ideas;
12+
13+
class live_search_test extends \phpbb\ideas\tests\ideas\ideas_base
14+
{
15+
public function live_search_data()
16+
{
17+
return [
18+
['Foo', [
19+
'Foo Idea #1 New with 3 up votes',
20+
'Foo Idea #3 In Progress with 1 up and 1 down vote',
21+
]],
22+
['bar', [
23+
'Bar Idea #2 New with 1 down vote',
24+
'Bar Idea #4 Implemented with 0 votes',
25+
]],
26+
['roved', [
27+
'Unapproved Idea #5 New with 0 votes',
28+
]],
29+
['xxx', []],
30+
];
31+
}
32+
33+
/**
34+
* @dataProvider live_search_data
35+
*/
36+
public function test_live_search($input, $expected)
37+
{
38+
$ideas = $this->get_ideas_object();
39+
40+
$results = $ideas->ideas_title_livesearch($input);
41+
42+
if (empty($expected))
43+
{
44+
$this->assertEmpty($results);
45+
}
46+
47+
foreach ($results as $result)
48+
{
49+
$this->assertContains($result['clean_title'], $expected);
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)