aboutsummaryrefslogtreecommitdiffstats
path: root/tests/functional/download_test.php
blob: 4e4995c21e0d883d9d256d92d43cf549d438e7ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
require_once dirname(__FILE__) . '/../../phpBB/includes/functions_content.php';
require_once dirname(__FILE__) . '/../../phpBB/includes/functions_posting.php';
require_once dirname(__FILE__) . '/../../phpBB/includes/utf/utf_tools.php';

/**
* @group functional
*/
class phpbb_functional_download_test extends phpbb_functional_test_case
{
	protected $data = array();

	public function test_setup_forums()
	{
		$this->login();
		$this->admin_login();

		$crawler = self::request('GET', "adm/index.php?i=acp_forums&mode=manage&sid={$this->sid}");
		$form = $crawler->selectButton('addforum')->form(array(
			'forum_name'	=> 'Download #1',
		));
		$crawler = self::submit($form);
		$form = $crawler->selectButton('update')->form(array(
			'forum_perm_from'	=> 2,
		));
		$crawler = self::submit($form);
	}

	public function test_create_post()
	{
		$this->login();
		$this->load_ids(array(
			'forums' => array(
				'Download #1',
			),
		));

		// Test creating topic
		$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));
		$crawler = self::request('GET', "viewtopic.php?t={$post['topic_id']}&sid={$this->sid}");

		$this->assertContains('Download Topic #1', $crawler->filter('html')->text());
		$this->data['topics']['Download Topic #1'] = (int) $post['topic_id'];
		$this->data['posts']['Download Topic #1'] = (int) $this->get_parameter_from_link($crawler->filter('.post')->selectLink($this->lang('POST', '', ''))->link()->getUri(), 'p');

		// Test creating a reply
		$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));
		$crawler = self::request('GET', "viewtopic.php?t={$post2['topic_id']}&sid={$this->sid}");

		$this->assertContains('Re: Download Topic #1-#2', $crawler->filter('html')->text());
		$this->data['posts']['Re: Download Topic #1-#2'] = (int) $post2['post_id'];
	}

	public function test_download_accessible()
	{
		$this->load_ids(array(
			'forums' => array(
				'Download #1',
			),
			'topics' => array(
				'Download Topic #1',
			),
			'posts' => array(
				'Download Topic #1',
				'Re: Download Topic #1-#2',
			),
			'attachments' => true,
		));

		// Download attachment as guest
		$crawler = self::request('GET', "download/file.php?id={$this->data['attachments'][$this->data['posts']['Re: Download Topic #1-#2']]}", array(), false);
		self::assert_response_status_code(200);
		$content = self::$client->getResponse()->getContent();
		$finfo = new finfo(FILEINFO_MIME_TYPE);
		self::assertEquals('image/jpeg', $finfo->buffer($content));
	}

	public function test_softdelete_post()
	{
		$this->login();
		$this->load_ids(array(
			'forums' => array(
				'Download #1',
			),
			'topics' => array(
				'Download Topic #1',
			),
			'posts' => array(
				'Download Topic #1',
				'Re: Download Topic #1-#2',
			),
		));
		$this->add_lang('posting');

		$crawler = self::request('GET', "posting.php?mode=delete&f={$this->data['forums']['Download #1']}&p={$this->data['posts']['Re: Download Topic #1-#2']}&sid={$this->sid}");
		$this->assertContainsLang('DELETE_PERMANENTLY', $crawler->text());

		$form = $crawler->selectButton('Yes')->form();
		$crawler = self::submit($form);
		$this->assertContainsLang('POST_DELETED', $crawler->text());

		$crawler = self::request('GET', "viewtopic.php?t={$this->data['topics']['Download Topic #1']}&sid={$this->sid}");
		$this->assertContains($this->lang('POST_DISPLAY', '', ''), $crawler->text());
	}

	public function test_download_softdeleted_post()
	{
		$this->load_ids(array(
			'forums' => array(
				'Download #1',
			),
			'topics' => array(
				'Download Topic #1',
			),
			'posts' => array(
				'Download Topic #1',
				'Re: Download Topic #1-#2',
			),
			'attachments' => true,
		));
		$this->add_lang('viewtopic');

		// No download attachment as guest
		$crawler = self::request('GET', "download/file.php?id={$this->data['attachments'][$this->data['posts']['Re: Download Topic #1-#2']]}", array(), false);
		self::assert_response_html(404);
		$this->assertContainsLang('ERROR_NO_ATTACHMENT', $crawler->filter('#message')->text());

		// Login as admin and try again, should work now.
		$this->login();

		// Download attachment as admin
		$crawler = self::request('GET', "download/file.php?id={$this->data['attachments'][$this->data['posts']['Re: Download Topic #1-#2']]}", array(), false);
		self::assert_response_status_code(200);
		$content = self::$client->getResponse()->getContent();
		$finfo = new finfo(FILEINFO_MIME_TYPE);
		self::assertEquals('image/jpeg', $finfo->buffer($content));
	}

	public function test_softdelete_topic()
	{
		$this->login();
		$this->load_ids(array(
			'forums' => array(
				'Download #1',
			),
			'topics' => array(
				'Download Topic #1',
			),
			'posts' => array(
				'Download Topic #1',
				'Re: Download Topic #1-#2',
			),
		));

		$this->add_lang('posting');
		$crawler = $this->get_quickmod_page($this->data['topics']['Download Topic #1'], 'DELETE_TOPIC');
		$this->assertContainsLang('DELETE_PERMANENTLY', $crawler->text());

		$this->add_lang('mcp');
		$form = $crawler->selectButton('Yes')->form();
		$crawler = self::submit($form);
		$this->assertContainsLang('TOPIC_DELETED_SUCCESS', $crawler->text());

		$crawler = self::request('GET', "viewtopic.php?t={$this->data['topics']['Download Topic #1']}&sid={$this->sid}");
		$this->assertContains('Download Topic #1', $crawler->filter('h2')->text());
	}

	public function test_download_softdeleted_topic()
	{
		$this->load_ids(array(
			'forums' => array(
				'Download #1',
			),
			'topics' => array(
				'Download Topic #1',
			),
			'posts' => array(
				'Download Topic #1',
				'Re: Download Topic #1-#2',
			),
			'attachments' => true,
		));
		$this->add_lang('viewtopic');

		// No download attachment as guest
		$crawler = self::request('GET', "download/file.php?id={$this->data['attachments'][$this->data['posts']['Re: Download Topic #1-#2']]}", array(), false);
		self::assert_response_html(404);
		$this->assertContainsLang('ERROR_NO_ATTACHMENT', $crawler->filter('#message')->text());

		// Login as admin and try again, should work now.
		$this->login();

		// Download attachment as admin
		$crawler = self::request('GET', "download/file.php?id={$this->data['attachments'][$this->data['posts']['Re: Download Topic #1-#2']]}", array(), false);
		self::assert_response_status_code(200);
		$content = self::$client->getResponse()->getContent();
		$finfo = new finfo(FILEINFO_MIME_TYPE);
		self::assertEquals('image/jpeg', $finfo->buffer($content));
	}

	public function load_ids($data)
	{
		$this->db = $this->get_db();

		if (!empty($data['forums']))
		{
			$sql = 'SELECT *
				FROM phpbb_forums
				WHERE ' . $this->db->sql_in_set('forum_name', $data['forums']);
			$result = $this->db->sql_query($sql);
			while ($row = $this->db->sql_fetchrow($result))
			{
				if (in_array($row['forum_name'], $data['forums']))
				{
					$this->data['forums'][$row['forum_name']] = (int) $row['forum_id'];
				}
			}
			$this->db->sql_freeresult($result);
		}

		if (!empty($data['topics']))
		{
			$sql = 'SELECT *
				FROM phpbb_topics
				WHERE ' . $this->db->sql_in_set('topic_title', $data['topics']);
			$result = $this->db->sql_query($sql);
			while ($row = $this->db->sql_fetchrow($result))
			{
				if (in_array($row['topic_title'], $data['topics']))
				{
					$this->data['topics'][$row['topic_title']] = (int) $row['topic_id'];
				}
			}
			$this->db->sql_freeresult($result);
		}

		$post_ids = array();
		if (!empty($data['posts']))
		{
			$sql = 'SELECT *
				FROM phpbb_posts
				WHERE ' . $this->db->sql_in_set('post_subject', $data['posts']);
			$result = $this->db->sql_query($sql);
			while ($row = $this->db->sql_fetchrow($result))
			{
				if (in_array($row['post_subject'], $data['posts']))
				{
					$this->data['posts'][$row['post_subject']] = (int) $row['post_id'];
					$post_ids[] = (int) $row['post_id'];
				}
			}
			$this->db->sql_freeresult($result);

			if (isset($data['attachments']))
			{
				$sql = 'SELECT *
					FROM phpbb_attachments
					WHERE in_message = 0 AND ' . $this->db->sql_in_set('post_msg_id', $post_ids);
				$result = $this->db->sql_query($sql);
				while ($row = $this->db->sql_fetchrow($result))
				{
					$this->data['attachments'][(int) $row['post_msg_id']] = (int) $row['attach_id'];
				}
				$this->db->sql_freeresult($result);
			}
		}
	}
}