Skip to content

Commit 7c17763

Browse files
authored
Merge pull request #92 from VSEphpbb/issue/91
Correctly handle moved ideas and idea counting
2 parents 87513eb + 5748ae1 commit 7c17763

3 files changed

Lines changed: 55 additions & 17 deletions

File tree

factory/ideas.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,19 @@ public function get_ideas($number = 10, $sort = 'date', $sort_direction = 'DESC'
148148
$where .= ' AND i.idea_votes_up > i.idea_votes_down';
149149
}
150150

151+
// Only get approved topics
152+
$where .= ' AND t.topic_visibility = ' . ITEM_APPROVED;
153+
154+
// Only get ideas that are actually in the ideas forum (not ones that have been moved)
155+
$where .= ' AND t.forum_id = ' . (int) $this->config['ideas_forum_id'];
156+
151157
// Count the total number of ideas for pagination
152158
if ($number >= $this->config['posts_per_page'])
153159
{
154160
$sql = 'SELECT COUNT(i.idea_id) as num_ideas
155-
FROM ' . $this->table_ideas . " i
161+
FROM ' . $this->table_ideas . ' i
162+
INNER JOIN ' . $this->table_topics . " t
163+
ON i.topic_id = t.topic_id
156164
WHERE $where";
157165
$result = $this->db->sql_query($sql);
158166
$num_ideas = (int) $this->db->sql_fetchfield('num_ideas');
@@ -162,9 +170,6 @@ public function get_ideas($number = 10, $sort = 'date', $sort_direction = 'DESC'
162170
$this->idea_count = $num_ideas;
163171
}
164172

165-
// Only get approved topics
166-
$where .= ' AND t.topic_visibility = ' . ITEM_APPROVED;
167-
168173
if ($sortby !== 'TOP' && $sortby !== 'ALL')
169174
{
170175
$sql = 'SELECT t.topic_last_post_time, t.topic_status, i.*
@@ -710,7 +715,8 @@ public function delete_orphans()
710715
// Find any orphans
711716
$sql = 'SELECT idea_id FROM ' . $this->table_ideas . '
712717
WHERE topic_id NOT IN (SELECT t.topic_id
713-
FROM ' . $this->table_topics . ' t)';
718+
FROM ' . $this->table_topics . ' t
719+
WHERE t.forum_id = ' . (int) $this->config['ideas_forum_id'] . ')';
714720
$result = $this->db->sql_query($sql);
715721
$rows = $this->db->sql_fetchrowset($result);
716722
$this->db->sql_freeresult($result);

tests/fixtures/ideas.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@
9797
<value></value>
9898
<value></value>
9999
</row>
100+
<row>
101+
<value>7</value>
102+
<value>2</value>
103+
<value>Orphaned Idea #7 New with no votes</value>
104+
<value>1439999994</value>
105+
<value>1</value>
106+
<value>1</value>
107+
<value>1</value>
108+
<value>7</value>
109+
<value>0</value>
110+
<value>0</value>
111+
<value></value>
112+
<value></value>
113+
</row>
100114
</table>
101115
<table name="phpbb_ideas_votes">
102116
<column>idea_id</column>
@@ -195,30 +209,42 @@
195209
<column>topic_id</column>
196210
<column>topic_title</column>
197211
<column>topic_visibility</column>
212+
<column>forum_id</column>
198213
<row>
199214
<value>1</value>
200215
<value>Foo Idea #1 New with 3 up votes</value>
201216
<value>1</value>
217+
<value>2</value>
202218
</row>
203219
<row>
204220
<value>2</value>
205221
<value>Bar Idea #2 New with 1 down vote</value>
206222
<value>1</value>
223+
<value>2</value>
207224
</row>
208225
<row>
209226
<value>3</value>
210227
<value>Foo Idea #3 In Progress with 1 up and 1 down vote</value>
211228
<value>1</value>
229+
<value>2</value>
212230
</row>
213231
<row>
214232
<value>4</value>
215233
<value>Bar Idea #4 Implemented with 0 votes</value>
216234
<value>1</value>
235+
<value>2</value>
217236
</row>
218237
<row>
219238
<value>5</value>
220239
<value>Unapproved Idea #5 New with 0 votes</value>
221240
<value>0</value>
241+
<value>2</value>
242+
</row>
243+
<row>
244+
<value>6</value>
245+
<value>Orphaned Idea #6 New with 2 votes</value>
246+
<value>1</value>
247+
<value>3</value>
222248
</row>
223249
</table>
224250
</dataset>

tests/ideas/delete_orphans_test.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,32 @@ class delete_orphans_test extends \phpbb\ideas\tests\ideas\ideas_base
1414
{
1515
public function test_delete_orphans()
1616
{
17-
// The id of an orphaned idea in our test fixture
18-
$idea_id = 6;
19-
2017
$ideas = $this->get_ideas_object();
2118

22-
// First, check the idea exists
23-
$this->assertNotEmpty($ideas->get_idea($idea_id));
19+
// First lets get a count of the good ideas
20+
$ideas->get_ideas();
21+
$valid_ideas = $ideas->get_idea_count();
22+
23+
// Check the orphan ideas exists
24+
$this->assertNotEmpty($ideas->get_idea(6));
25+
$this->assertNotEmpty($ideas->get_idea(7));
2426

2527
// Delete orphans
26-
$this->assertEquals(1, $ideas->delete_orphans());
28+
$this->assertEquals(2, $ideas->delete_orphans());
2729

28-
// Confirm idea no longer exists
29-
$this->assertFalse($ideas->get_idea($idea_id));
30+
// Confirm orphan ideas no longer exists
31+
$this->assertFalse($ideas->get_idea(6));
32+
$this->assertFalse($ideas->get_idea(7));
3033

31-
// check that all votes are removed
32-
$sql = 'SELECT * FROM phpbb_ideas_votes WHERE idea_id = ' . $idea_id;
34+
// Check that all votes from orphan ideas are removed
35+
$sql = 'SELECT COUNT(idea_id) as num_ideas FROM phpbb_ideas_votes WHERE idea_id IN(6, 7)';
3336
$result = $this->db->sql_query($sql);
34-
$rows = $this->db->sql_fetchrow($result);
37+
$num_ideas = (int) $this->db->sql_fetchfield('num_ideas');
3538
$this->db->sql_freeresult($result);
39+
$this->assertEquals(0, $num_ideas);
3640

37-
$this->assertFalse($rows);
41+
// Confirm that only the orphans were deleted
42+
$ideas->get_ideas();
43+
$this->assertEquals($valid_ideas, $ideas->get_idea_count());
3844
}
3945
}

0 commit comments

Comments
 (0)