Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 187
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_functional_download_test
0.00% covered (danger)
0.00%
0 / 187
0.00% covered (danger)
0.00%
0 / 8
552
0.00% covered (danger)
0.00%
0 / 1
 test_setup_forums
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 test_create_post
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
2
 test_download_accessible
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
6
 test_softdelete_post
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
2
 test_download_softdeleted_post
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
6
 test_softdelete_topic
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
2
 test_download_softdeleted_topic
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
6
 load_ids
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 1
182
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
14require_once __DIR__ . '/../../phpBB/includes/functions_posting.php';
15
16/**
17* @group functional
18*/
19class phpbb_functional_download_test extends phpbb_functional_test_case
20{
21    protected $data = array();
22
23    public function test_setup_forums()
24    {
25        $this->login();
26        $this->admin_login();
27
28        $crawler = self::request('GET', "adm/index.php?i=acp_forums&mode=manage&sid={$this->sid}");
29        $form = $crawler->selectButton('addforum')->form(array(
30            'forum_name'    => 'Download #1',
31        ));
32        $crawler = self::submit($form);
33        $form = $crawler->selectButton('update')->form(array(
34            'forum_perm_from'    => 2,
35        ));
36        $crawler = self::submit($form);
37    }
38
39    public function test_create_post()
40    {
41        $this->login();
42        $this->load_ids(array(
43            'forums' => array(
44                'Download #1',
45            ),
46        ));
47
48        // Test creating topic
49        $post = $this->create_topic($this->data['forums']['Download #1'], 'Download Topic #1', 'This is a test topic posted by the testing framework.', array('upload_files' => 1));
50        $crawler = self::request('GET', "viewtopic.php?t={$post['topic_id']}&sid={$this->sid}");
51
52        $this->assertStringContainsString('Download Topic #1', $crawler->filter('html')->text());
53        $this->data['topics']['Download Topic #1'] = (int) $post['topic_id'];
54        $this->data['posts']['Download Topic #1'] = (int) $this->get_parameter_from_link($crawler->filter('.post')->selectLink($this->lang('POST', '', ''))->link()->getUri(), 'p');
55
56        // Test creating a reply
57        $post2 = $this->create_post($this->data['forums']['Download #1'], $post['topic_id'], 'Re: Download Topic #1-#2', 'This is a test post posted by the testing framework.', array('upload_files' => 1));
58        $crawler = self::request('GET', "viewtopic.php?p={$post2['post_id']}&sid={$this->sid}");
59
60        $this->assertStringContainsString('Re: Download Topic #1-#2', $crawler->filter('html')->text());
61        $this->data['posts']['Re: Download Topic #1-#2'] = (int) $post2['post_id'];
62    }
63
64    public function test_download_accessible()
65    {
66        if (!class_exists('finfo'))
67        {
68            $this->markTestSkipped('Unable to run test with fileinfo disabled');
69        }
70
71        $this->load_ids(array(
72            'forums' => array(
73                'Download #1',
74            ),
75            'topics' => array(
76                'Download Topic #1',
77            ),
78            'posts' => array(
79                'Download Topic #1',
80                'Re: Download Topic #1-#2',
81            ),
82            'attachments' => true,
83        ));
84
85        // Download attachment as guest
86        $crawler = self::request('GET', "app.php/download/attachment/{$this->data['attachments'][$this->data['posts']['Re: Download Topic #1-#2']]}", array(), false);
87        self::assert_response_status_code(200);
88        $content = self::$client->getResponse()->getContent();
89        $finfo = new finfo(FILEINFO_MIME_TYPE);
90        self::assertEquals('image/jpeg', $finfo->buffer($content));
91    }
92
93    public function test_softdelete_post()
94    {
95        $this->login();
96        $this->load_ids(array(
97            'forums' => array(
98                'Download #1',
99            ),
100            'topics' => array(
101                'Download Topic #1',
102            ),
103            'posts' => array(
104                'Download Topic #1',
105                'Re: Download Topic #1-#2',
106            ),
107        ));
108        $this->add_lang('posting');
109
110        $crawler = self::request('GET', "posting.php?mode=delete&p={$this->data['posts']['Re: Download Topic #1-#2']}&sid={$this->sid}");
111        $this->assertContainsLang('DELETE_PERMANENTLY', $crawler->text());
112
113        $form = $crawler->selectButton('Yes')->form();
114        $crawler = self::submit($form);
115        $this->assertContainsLang('POST_DELETED', $crawler->text());
116
117        $crawler = self::request('GET', "viewtopic.php?t={$this->data['topics']['Download Topic #1']}&sid={$this->sid}");
118        $this->assertStringContainsString($this->lang('POST_DISPLAY'), $crawler->text());
119    }
120
121    public function test_download_softdeleted_post()
122    {
123        if (!class_exists('finfo'))
124        {
125            $this->markTestSkipped('Unable to run test with fileinfo disabled');
126        }
127
128        $this->load_ids(array(
129            'forums' => array(
130                'Download #1',
131            ),
132            'topics' => array(
133                'Download Topic #1',
134            ),
135            'posts' => array(
136                'Download Topic #1',
137                'Re: Download Topic #1-#2',
138            ),
139            'attachments' => true,
140        ));
141        $this->add_lang('viewtopic');
142
143        // No download attachment as guest
144        $crawler = self::request('GET', "app.php/download/attachment/{$this->data['attachments'][$this->data['posts']['Re: Download Topic #1-#2']]}", array(), false);
145        self::assert_response_html(404);
146        $this->assertContainsLang('ERROR_NO_ATTACHMENT', $crawler->filter('#message')->text());
147
148        // Login as admin and try again, should work now.
149        $this->login();
150
151        // Download attachment as admin
152        $crawler = self::request('GET', "app.php/download/attachment/{$this->data['attachments'][$this->data['posts']['Re: Download Topic #1-#2']]}", array(), false);
153        self::assert_response_status_code(200);
154        $content = self::$client->getResponse()->getContent();
155        $finfo = new finfo(FILEINFO_MIME_TYPE);
156        self::assertEquals('image/jpeg', $finfo->buffer($content));
157    }
158
159    public function test_softdelete_topic()
160    {
161        $this->login();
162        $this->load_ids(array(
163            'forums' => array(
164                'Download #1',
165            ),
166            'topics' => array(
167                'Download Topic #1',
168            ),
169            'posts' => array(
170                'Download Topic #1',
171                'Re: Download Topic #1-#2',
172            ),
173        ));
174
175        $this->add_lang('posting');
176        $crawler = $this->get_quickmod_page($this->data['topics']['Download Topic #1'], 'DELETE_TOPIC');
177        $this->assertContainsLang('DELETE_PERMANENTLY', $crawler->text());
178
179        $this->add_lang('mcp');
180        $form = $crawler->selectButton('Yes')->form();
181        $crawler = self::submit($form);
182        $this->assertContainsLang('TOPIC_DELETED_SUCCESS', $crawler->text());
183
184        $crawler = self::request('GET', "viewtopic.php?t={$this->data['topics']['Download Topic #1']}&sid={$this->sid}");
185        $this->assertStringContainsString('Download Topic #1', $crawler->filter('h2')->text());
186    }
187
188    public function test_download_softdeleted_topic()
189    {
190        if (!class_exists('finfo'))
191        {
192            $this->markTestSkipped('Unable to run test with fileinfo disabled');
193        }
194
195        $this->load_ids(array(
196            'forums' => array(
197                'Download #1',
198            ),
199            'topics' => array(
200                'Download Topic #1',
201            ),
202            'posts' => array(
203                'Download Topic #1',
204                'Re: Download Topic #1-#2',
205            ),
206            'attachments' => true,
207        ));
208        $this->add_lang('viewtopic');
209
210        // No download attachment as guest
211        $crawler = self::request('GET', "app.php/download/attachment/{$this->data['attachments'][$this->data['posts']['Re: Download Topic #1-#2']]}", array(), false);
212        self::assert_response_html(404);
213        $this->assertContainsLang('ERROR_NO_ATTACHMENT', $crawler->filter('#message')->text());
214
215        // Login as admin and try again, should work now.
216        $this->login();
217
218        // Download attachment as admin
219        $crawler = self::request('GET', "app.php/download/attachment/{$this->data['attachments'][$this->data['posts']['Re: Download Topic #1-#2']]}", array(), false);
220        self::assert_response_status_code(200);
221        $content = self::$client->getResponse()->getContent();
222        $finfo = new finfo(FILEINFO_MIME_TYPE);
223        self::assertEquals('image/jpeg', $finfo->buffer($content));
224    }
225
226    public function load_ids($data)
227    {
228        if (!empty($data['forums']))
229        {
230            array_walk($data['forums'], function(&$value, $key)
231                {
232                    $value = $this->db->sql_escape($value);
233                }
234            );
235            $sql = 'SELECT *
236                FROM phpbb_forums
237                WHERE ' . $this->db->sql_in_set('forum_name', $data['forums']);
238            $result = $this->db->sql_query($sql);
239            while ($row = $this->db->sql_fetchrow($result))
240            {
241                if (in_array($row['forum_name'], $data['forums']))
242                {
243                    $this->data['forums'][$row['forum_name']] = (int) $row['forum_id'];
244                }
245            }
246            $this->db->sql_freeresult($result);
247        }
248
249        if (!empty($data['topics']))
250        {
251            array_walk($data['topics'], function(&$value, $key)
252                {
253                    $value = $this->db->sql_escape($value);
254                }
255            );
256            $sql = 'SELECT *
257                FROM phpbb_topics
258                WHERE ' . $this->db->sql_in_set('topic_title', $data['topics']);
259            $result = $this->db->sql_query($sql);
260            while ($row = $this->db->sql_fetchrow($result))
261            {
262                if (in_array($row['topic_title'], $data['topics']))
263                {
264                    $this->data['topics'][$row['topic_title']] = (int) $row['topic_id'];
265                }
266            }
267            $this->db->sql_freeresult($result);
268        }
269
270        $post_ids = array();
271        if (!empty($data['posts']))
272        {
273            array_walk($data['posts'], function(&$value, $key)
274                {
275                    $value = $this->db->sql_escape($value);
276                }
277            );
278            $sql = 'SELECT *
279                FROM phpbb_posts
280                WHERE ' . $this->db->sql_in_set('post_subject', $data['posts']);
281            $result = $this->db->sql_query($sql);
282            while ($row = $this->db->sql_fetchrow($result))
283            {
284                if (in_array($row['post_subject'], $data['posts']))
285                {
286                    $this->data['posts'][$row['post_subject']] = (int) $row['post_id'];
287                    $post_ids[] = (int) $row['post_id'];
288                }
289            }
290            $this->db->sql_freeresult($result);
291
292            if (isset($data['attachments']) && !empty($post_ids))
293            {
294                $sql = 'SELECT *
295                    FROM phpbb_attachments
296                    WHERE in_message = 0 AND ' . $this->db->sql_in_set('post_msg_id', $post_ids);
297                $result = $this->db->sql_query($sql);
298                while ($row = $this->db->sql_fetchrow($result))
299                {
300                    $this->data['attachments'][(int) $row['post_msg_id']] = (int) $row['attach_id'];
301                }
302                $this->db->sql_freeresult($result);
303            }
304        }
305    }
306}