Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 145
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
viewforum_paging_test
0.00% covered (danger)
0.00%
0 / 145
0.00% covered (danger)
0.00%
0 / 7
342
0.00% covered (danger)
0.00%
0 / 1
 test_setup_forums
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
2
 test_create_posts
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
6
 test_viewforum_first_page
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
2
 test_viewforum_second_page
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
2
 assert_forum_details
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 set_post_settings
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 load_ids
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2/**
3*
4* This file is part of 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* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14/**
15* @group functional
16*/
17class viewforum_paging_test extends phpbb_functional_test_case
18{
19    protected $data = array();
20
21    public function test_setup_forums()
22    {
23        $this->login();
24        $this->admin_login();
25
26        $crawler = self::request('GET', "adm/index.php?i=acp_forums&mode=manage&sid={$this->sid}");
27        $form = $crawler->selectButton('addforum')->form(array(
28            'forum_name'    => 'Viewforum Pagination Test #1',
29        ));
30        $crawler = self::submit($form);
31        $form = $crawler->selectButton('update')->form(array(
32            'forum_perm_from'    => 2,
33        ));
34        self::submit($form);
35
36        $crawler = self::request('GET', "adm/index.php?i=acp_forums&mode=manage&sid={$this->sid}");
37        $form = $crawler->selectButton('addforum')->form(array(
38            'forum_name'    => 'Viewforum Pagination Test #2',
39        ));
40        $crawler = self::submit($form);
41        $form = $crawler->selectButton('update')->form(array(
42            'forum_perm_from'    => 2,
43        ));
44        self::submit($form);
45
46        $this->set_post_settings(array(
47            'flood_interval'    => 0,
48            'topics_per_page'    => 3,
49        ));
50    }
51
52    public function test_create_posts()
53    {
54        $this->login();
55        $this->load_ids(array(
56            'forums' => array(
57                'Viewforum Pagination Test #1',
58                'Viewforum Pagination Test #2',
59            ),
60        ));
61
62        $this->assert_forum_details($this->data['forums']['Viewforum Pagination Test #1'], array(
63            'forum_posts_approved'        => 0,
64            'forum_posts_unapproved'    => 0,
65            'forum_posts_softdeleted'    => 0,
66            'forum_topics_approved'        => 0,
67            'forum_topics_unapproved'    => 0,
68            'forum_topics_softdeleted'    => 0,
69            'forum_last_post_id'        => 0,
70        ), 'initial comparison');
71
72        for ($topic_id = 1; $topic_id <= 6; $topic_id++)
73        {
74            $this->create_topic($this->data['forums']['Viewforum Pagination Test #1'], 'Viewforum Pagination TestTopic #' . $topic_id, 'This is a test topic posted by the testing framework.');
75        }
76
77        $this->create_topic($this->data['forums']['Viewforum Pagination Test #2'], 'Viewforum Pagination TestTopic #GA1', 'This is a test topic posted by the testing framework.', array(
78            'topic_type' => POST_GLOBAL,
79        ));
80
81        $this->assert_forum_details($this->data['forums']['Viewforum Pagination Test #1'], array(
82            'forum_posts_approved'        => 6,
83            'forum_posts_unapproved'    => 0,
84            'forum_posts_softdeleted'    => 0,
85            'forum_topics_approved'        => 6,
86            'forum_topics_unapproved'    => 0,
87            'forum_topics_softdeleted'    => 0,
88        ), 'after creating topics');
89
90        $this->assert_forum_details($this->data['forums']['Viewforum Pagination Test #2'], array(
91            'forum_posts_approved'        => 1,
92            'forum_posts_unapproved'    => 0,
93            'forum_posts_softdeleted'    => 0,
94            'forum_topics_approved'        => 1,
95            'forum_topics_unapproved'    => 0,
96            'forum_topics_softdeleted'    => 0,
97        ), 'after creating GA');
98
99        // Set flood interval back to 15
100        $this->admin_login();
101        $this->set_post_settings(array(
102            'flood_interval'    => 15,
103        ));
104    }
105
106    public function test_viewforum_first_page()
107    {
108        $this->load_ids(array(
109            'forums' => array(
110                'Viewforum Pagination Test #1',
111                'Viewforum Pagination Test #2',
112            ),
113        ));
114        $crawler = self::request('GET', 'viewforum.php?f=' . $this->data['forums']['Viewforum Pagination Test #1']);
115
116        // Test the topics that are displayed
117        $topiclists = $crawler->filter('.forumbg .topics');
118        $this->assertEquals(2, $topiclists->count());
119        $topiclist = $topiclists->eq(0)->filter('li');
120        $this->assertStringEndsWith('TestTopic #GA1', $topiclist->eq(0)->filter('.topictitle')->text());
121        $topiclist = $topiclists->eq(1)->filter('li');
122        $this->assertStringEndsWith('TestTopic #6', $topiclist->eq(0)->filter('.topictitle')->text());
123        $this->assertStringEndsWith('TestTopic #5', $topiclist->eq(1)->filter('.topictitle')->text());
124        $this->assertStringEndsWith('TestTopic #4', $topiclist->eq(2)->filter('.topictitle')->text());
125
126        // Test the pagination, should only have: 1 - 2 - Next
127        $this->assertEquals(2, $crawler->filter('div.pagination')->count());
128        $top_pagination = $crawler->filter('div.pagination')->eq(0);
129        $this->assertEquals(3, $top_pagination->filter('li')->count(), 'Number of pagination items on page 1 does not match');
130        $this->assertStringContainsString('1', $top_pagination->filter('li')->eq(0)->text());
131        $this->assertStringContainsString('2', $top_pagination->filter('li')->eq(1)->text());
132        $this->assertContainsLang('NEXT', $top_pagination->filter('li')->eq(2)->text());
133    }
134
135    public function test_viewforum_second_page()
136    {
137        $this->load_ids(array(
138            'forums' => array(
139                'Viewforum Pagination Test #1',
140                'Viewforum Pagination Test #2',
141            ),
142        ));
143        $crawler = self::request('GET', 'viewforum.php?f=' . $this->data['forums']['Viewforum Pagination Test #1'] . '&start=3');
144
145        // Test the topics that are displayed
146        $topiclists = $crawler->filter('.forumbg .topics');
147        $this->assertEquals(2, $topiclists->count());
148        $topiclist = $topiclists->eq(0)->filter('li');
149        $this->assertStringEndsWith('TestTopic #GA1', $topiclist->eq(0)->filter('.topictitle')->text());
150        $topiclist = $topiclists->eq(1)->filter('li');
151        $this->assertStringEndsWith('TestTopic #3', $topiclist->eq(0)->filter('.topictitle')->text());
152        $this->assertStringEndsWith('TestTopic #2', $topiclist->eq(1)->filter('.topictitle')->text());
153        $this->assertStringEndsWith('TestTopic #1', $topiclist->eq(2)->filter('.topictitle')->text());
154
155        // Test the pagination, should only have: Previous - 1 - 2
156        $this->assertEquals(2, $crawler->filter('div.pagination')->count());
157        $top_pagination = $crawler->filter('div.pagination')->eq(0);
158        $this->assertEquals(3, $top_pagination->filter('li')->count(), 'Number of pagination items on page 2 does not match');
159        $this->assertContainsLang('PREVIOUS', $top_pagination->filter('li')->eq(0)->text());
160        $this->assertStringContainsString('1', $top_pagination->filter('li')->eq(1)->text());
161        $this->assertStringContainsString('2', $top_pagination->filter('li')->eq(2)->text());
162    }
163
164    protected function assert_forum_details($forum_id, $details, $additional_error_message = '')
165    {
166        $sql = 'SELECT ' . implode(', ', array_keys($details)) . '
167            FROM phpbb_forums
168            WHERE forum_id = ' . (int) $forum_id;
169        $result = $this->db->sql_query($sql);
170        $data = $this->db->sql_fetchrow($result);
171        $this->db->sql_freeresult($result);
172
173        $this->assertEquals($details, $data, "Forum {$forum_id} does not match expected {$additional_error_message}");
174    }
175
176    /**
177     * Sets the post setting via the ACP page
178     *
179     * @param array $settings
180     */
181    protected function set_post_settings($settings)
182    {
183        $crawler = self::request('GET', 'adm/index.php?sid=' . $this->sid . '&i=acp_board&mode=post');
184
185        $form = $crawler->selectButton('Submit')->form();
186        $values = $form->getValues();
187
188        foreach ($settings as $setting => $value)
189        {
190            $values["config[{$setting}]"] = $value;
191        }
192        $form->setValues($values);
193        $crawler = self::submit($form);
194        $this->assertGreaterThan(0, $crawler->filter('.successbox')->count());
195    }
196
197    /**
198     * Loads forum, topic and post IDs
199     *
200     * @param array $data
201     */
202    protected function load_ids($data)
203    {
204        if (!empty($data['forums']))
205        {
206            $sql = 'SELECT *
207                FROM phpbb_forums
208                WHERE ' . $this->db->sql_in_set('forum_name', $data['forums']);
209            $result = $this->db->sql_query($sql);
210            while ($row = $this->db->sql_fetchrow($result))
211            {
212                if (in_array($row['forum_name'], $data['forums']))
213                {
214                    $this->data['forums'][$row['forum_name']] = (int) $row['forum_id'];
215                }
216            }
217            $this->db->sql_freeresult($result);
218        }
219
220        if (!empty($data['topics']))
221        {
222            $sql = 'SELECT *
223                FROM phpbb_topics
224                WHERE ' . $this->db->sql_in_set('topic_title', $data['topics']);
225            $result = $this->db->sql_query($sql);
226            while ($row = $this->db->sql_fetchrow($result))
227            {
228                if (in_array($row['topic_title'], $data['topics']))
229                {
230                    $this->data['topics'][$row['topic_title']] = (int) $row['topic_id'];
231                }
232            }
233            $this->db->sql_freeresult($result);
234        }
235
236        if (!empty($data['posts']))
237        {
238            $sql = 'SELECT *
239                FROM phpbb_posts
240                WHERE ' . $this->db->sql_in_set('post_subject', $data['posts']);
241            $result = $this->db->sql_query($sql);
242            while ($row = $this->db->sql_fetchrow($result))
243            {
244                if (in_array($row['post_subject'], $data['posts']))
245                {
246                    $this->data['posts'][$row['post_subject']] = (int) $row['post_id'];
247                }
248            }
249            $this->db->sql_freeresult($result);
250        }
251    }
252}