aboutsummaryrefslogtreecommitdiffstats
path: root/tests/functional/visibility_unapproved_posts_test.php
blob: 2fb2ef3403451d84d42324238497cc1c6d8898c2 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?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.
*
*/

/**
* @group functional
*/
class phpbb_functional_visibility_unapproved_test extends phpbb_functional_test_case
{
	protected $data = [];

	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([
			'forum_name'	=> 'Unapproved Posts Test #1',
		]);
		$crawler = self::submit($form);
		$form = $crawler->selectButton('update')->form([
			'forum_perm_from'	=> 2,
		]);
		$crawler = self::submit($form);

		// Set flood interval to 0
		$this->set_flood_interval(0);
	}

	public function test_create_posts()
	{
		$this->login();
		$this->load_ids([
			'forums' => [
				'Unapproved Posts Test #1',
			],
		]);

		$this->assert_forum_details($this->data['forums']['Unapproved Posts Test #1'], [
			'forum_posts_approved'		=> 0,
			'forum_posts_unapproved'	=> 0,
			'forum_posts_softdeleted'	=> 0,
			'forum_topics_approved'		=> 0,
			'forum_topics_unapproved'	=> 0,
			'forum_topics_softdeleted'	=> 0,
			'forum_last_post_id'		=> 0,
		], 'initial comparison');

		// Test creating topic #1
		$post = $this->create_topic($this->data['forums']['Unapproved Posts Test #1'], 'Unapproved Posts Test Topic #1', 'This is a test topic posted by the testing framework.');
		$crawler = self::request('GET', "viewtopic.php?t={$post['topic_id']}&sid={$this->sid}");

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

		$this->assert_forum_details($this->data['forums']['Unapproved Posts Test #1'], [
			'forum_posts_approved'		=> 1,
			'forum_posts_unapproved'	=> 0,
			'forum_posts_softdeleted'	=> 0,
			'forum_topics_approved'		=> 1,
			'forum_topics_unapproved'	=> 0,
			'forum_topics_softdeleted'	=> 0,
			'forum_last_post_id'		=> $this->data['posts']['Unapproved Posts Test Topic #1'],
		], 'after creating topic #1');

		$this->logout();
		$this->create_user('unapproved_posts_test_user#1');
		$this->add_user_group('NEWLY_REGISTERED', ['unapproved_posts_test_user#1']);
		$this->login('unapproved_posts_test_user#1');

		// Test creating a reply
		$post2 = $this->create_post($this->data['forums']['Unapproved Posts Test #1'], $post['topic_id'], 'Re: Unapproved Posts Test Topic #1-#2', 'This is a test post posted by the testing framework.', [], 'POST_STORED_MOD');

		$crawler = self::request('GET', "viewtopic.php?t={$this->data['topics']['Unapproved Posts Test Topic #1']}&sid={$this->sid}");
		$this->assertNotContains('Re: Unapproved Posts Test Topic #1-#2', $crawler->filter('#page-body')->text());

		$this->assert_forum_details($this->data['forums']['Unapproved Posts Test #1'], [
			'forum_posts_approved'		=> 1,
			'forum_posts_unapproved'	=> 1,
			'forum_posts_softdeleted'	=> 0,
			'forum_topics_approved'		=> 1,
			'forum_topics_unapproved'	=> 0,
			'forum_topics_softdeleted'	=> 0,
			'forum_last_post_id'		=> $this->data['posts']['Unapproved Posts Test Topic #1'],
		], 'after replying');

		// Test creating topic #2
		$post = $this->create_topic($this->data['forums']['Unapproved Posts Test #1'], 'Unapproved Posts Test Topic #2', 'This is a test topic posted by the testing framework.', [], 'POST_STORED_MOD');
		$crawler = self::request('GET', "viewforum.php?f={$this->data['forums']['Unapproved Posts Test #1']}&sid={$this->sid}");

		$this->assertNotContains('Unapproved Posts Test Topic #2', $crawler->filter('html')->text());

		$this->assert_forum_details($this->data['forums']['Unapproved Posts Test #1'], [
			'forum_posts_approved'		=> 1,
			'forum_posts_unapproved'	=> 2,
			'forum_posts_softdeleted'	=> 0,
			'forum_topics_approved'		=> 1,
			'forum_topics_unapproved'	=> 1,
			'forum_topics_softdeleted'	=> 0,
			'forum_last_post_id'		=> $this->data['posts']['Unapproved Posts Test Topic #1'],
		], 'after creating topic #2');

		$this->logout();
	}

	public function test_view_unapproved_post_disabled()
	{
		// user who created post
		$this->login('unapproved_posts_test_user#1');
		$this->load_ids([
			'forums' => [
				'Unapproved Posts Test #1',
			],
			'topics' => [
				'Unapproved Posts Test Topic #1',
				'Unapproved Posts Test Topic #2',
			],
			'posts' => [
				'Unapproved Posts Test Topic #1',
				'Re: Unapproved Posts Test Topic #1-#2',
				'Unapproved Posts Test Topic #2',
			],
		]);

		$this->assert_forum_details($this->data['forums']['Unapproved Posts Test #1'], [
			'forum_posts_approved'		=> 1,
			'forum_posts_unapproved'	=> 2,
			'forum_posts_softdeleted'	=> 0,
			'forum_topics_approved'		=> 1,
			'forum_topics_unapproved'	=> 1,
			'forum_topics_softdeleted'	=> 0,
			'forum_last_post_id'		=> $this->data['posts']['Unapproved Posts Test Topic #1'],
		], 'before approving post');

		$this->add_lang('posting');
		$this->add_lang('viewtopic');
		$this->add_lang('mcp');

		// should be able to see topic 1 but not unapproved post
		$crawler = self::request('GET', "viewtopic.php?t={$this->data['topics']['Unapproved Posts Test Topic #1']}&sid={$this->sid}");
		$this->assertContains('Unapproved Posts Test Topic #1', $crawler->filter('h2')->text());
		$this->assertNotContains('Re: Unapproved Posts Test Topic #1-#2', $crawler->filter('#page-body')->text());
		$this->assertNotContains('This post is not visible to other users until it has been approved', $crawler->filter('#page-body')->text());

		// should not be able to see topic 2
		$crawler = self::request('GET', "viewforum.php?f={$this->data['forums']['Unapproved Posts Test #1']}&sid={$this->sid}");
		$this->assertNotContains('Unapproved Posts Test Topic #2', $crawler->filter('html')->text());
		$this->logout();

		// another user
		$this->create_user('unapproved_posts_test_user#2');
		$this->login('unapproved_posts_test_user#2');

		$this->add_lang('posting', 'viewtopic', 'mcp');

		// should be able to see topic 1 but not unapproved post
		$crawler = self::request('GET', "viewtopic.php?t={$this->data['topics']['Unapproved Posts Test Topic #1']}&sid={$this->sid}");
		$this->assertContains('Unapproved Posts Test Topic #1', $crawler->filter('h2')->text());
		$this->assertNotContains('Re: Unapproved Posts Test Topic #1-#2', $crawler->filter('#page-body')->text());
		$this->assertNotContains('This post is not visible to other users until it has been approved', $crawler->filter('#page-body')->text());

		// should not be able to see topic 2
		$crawler = self::request('GET', "viewforum.php?f={$this->data['forums']['Unapproved Posts Test #1']}&sid={$this->sid}");
		$this->assertNotContains('Unapproved Posts Test Topic #2', $crawler->filter('html')->text());
	}

	public function test_view_unapproved_post_enabled()
	{
		$this->config_display_unapproved_posts_state(true);

		// user who created post
		$this->login('unapproved_posts_test_user#1');
		$this->load_ids([
			'forums' => [
				'Unapproved Posts Test #1',
			],
			'topics' => [
				'Unapproved Posts Test Topic #1',
				'Unapproved Posts Test Topic #2',
			],
			'posts' => [
				'Unapproved Posts Test Topic #1',
				'Re: Unapproved Posts Test Topic #1-#2',
				'Unapproved Posts Test Topic #2',
			],
		]);

		$this->assert_forum_details($this->data['forums']['Unapproved Posts Test #1'], [
			'forum_posts_approved'		=> 1,
			'forum_posts_unapproved'	=> 2,
			'forum_posts_softdeleted'	=> 0,
			'forum_topics_approved'		=> 1,
			'forum_topics_unapproved'	=> 1,
			'forum_topics_softdeleted'	=> 0,
			'forum_last_post_id'		=> $this->data['posts']['Unapproved Posts Test Topic #1'],
		], 'before approving post');

		$this->add_lang('posting');
		$this->add_lang('viewtopic');
		$this->add_lang('mcp');

		// should be able to see topic 1 and unapproved post
		$crawler = self::request('GET', "viewtopic.php?t={$this->data['topics']['Unapproved Posts Test Topic #1']}&sid={$this->sid}");
		$this->assertContains('Unapproved Posts Test Topic #1', $crawler->filter('h2')->text());
		$this->assertContains('Re: Unapproved Posts Test Topic #1-#2', $crawler->filter('#page-body')->text());
		$this->assertContains('This post is not visible to other users until it has been approved', $crawler->filter('#page-body')->text());

		// should be able to see topic 2
		$crawler = self::request('GET', "viewforum.php?f={$this->data['forums']['Unapproved Posts Test #1']}&sid={$this->sid}");
		$this->assertContains('Unapproved Posts Test Topic #2', $crawler->filter('html')->text());

		// should be able to see post in topic 2
		$crawler = self::request('GET', "viewtopic.php?t={$this->data['topics']['Unapproved Posts Test Topic #2']}&sid={$this->sid}");
		$this->assertContains('Unapproved Posts Test Topic #2', $crawler->filter('#page-body')->text());
		$this->assertContains('This post is not visible to other users until it has been approved', $crawler->filter('#page-body')->text());
		$this->logout();

		// another user
		$this->login('unapproved_posts_test_user#2');

		$this->add_lang('posting');
		$this->add_lang('viewtopic');
		$this->add_lang('mcp');

		// should be able to see topic 1 but not unapproved post
		$crawler = self::request('GET', "viewtopic.php?t={$this->data['topics']['Unapproved Posts Test Topic #1']}&sid={$this->sid}");
		$this->assertContains('Unapproved Posts Test Topic #1', $crawler->filter('h2')->text());
		$this->assertNotContains('Re: Unapproved Posts Test Topic #1-#2', $crawler->filter('#page-body')->text());
		$this->assertNotContains('This post is not visible to other users until it has been approved', $crawler->filter('#page-body')->text());

		// should not be able to see topic 2
		$crawler = self::request('GET', "viewforum.php?f={$this->data['forums']['Unapproved Posts Test #1']}&sid={$this->sid}");
		$this->assertNotContains('Unapproved Posts Test Topic #2', $crawler->filter('html')->text());
		$this->logout();

		// revert the configuration (if necessary)
		//$this->config_display_unapproved_posts_state(false);
	}

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

		// Set flood interval back to 15
		$this->set_flood_interval(15);
	}

	protected function assert_forum_details($forum_id, $details, $additional_error_message = '')
	{
		$this->db = $this->get_db();

		$sql = 'SELECT ' . implode(', ', array_keys($details)) . '
			FROM phpbb_forums
			WHERE forum_id = ' . (int) $forum_id;
		$result = $this->db->sql_query($sql);
		$data = $this->db->sql_fetchrow($result);
		$this->db->sql_freeresult($result);

		$this->assertEquals($details, $data, "Forum {$forum_id} does not match expected {$additional_error_message}");
	}

	protected function set_flood_interval($flood_interval)
	{
		$crawler = self::request('GET', "adm/index.php?sid={$this->sid}&i=acp_board&mode=post");

		$form = $crawler->selectButton('Submit')->form();
		$values = $form->getValues();

		$values['config[flood_interval]'] = $flood_interval;
		$form->setValues($values);
		$crawler = self::submit($form);
		$this->assertGreaterThan(0, $crawler->filter('.successbox')->count());
	}

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

		if (!empty($data['forums']))
		{
			$sql = 'SELECT forum_id, forum_name
				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);
		}

		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'];
				}
			}
			$this->db->sql_freeresult($result);
		}
	}

	protected function config_display_unapproved_posts_state($state)
	{
		$this->login();
		$this->admin_login();

		$crawler = self::request('GET', "adm/index.php?sid={$this->sid}&i=acp_board&mode=features");

		$form = $crawler->selectButton('Submit')->form();
		$values = $form->getValues();

		// Enable display of unapproved posts to posters
		$values['config[display_unapproved_posts]'] = $state;

		$form->setValues($values);

		$crawler = self::submit($form);
		self::assertContainsLang('CONFIG_UPDATED', $crawler->filter('.successbox')->text());
		$this->logout();
	}
}