Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 128
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_functional_prune_shadow_topic_test
0.00% covered (danger)
0.00%
0 / 128
0.00% covered (danger)
0.00%
0 / 5
240
0.00% covered (danger)
0.00%
0 / 1
 test_setup_forums
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
2
 test_create_post
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 1
2
 test_move_topic
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 1
6
 assert_forum_details
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 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 phpbb_functional_prune_shadow_topic_test extends phpbb_functional_test_case
18{
19    protected $data = array();
20    protected $post;
21
22    public function test_setup_forums()
23    {
24        $this->login();
25        $this->admin_login();
26
27        $crawler = self::request('GET', "adm/index.php?i=acp_forums&mode=manage&sid={$this->sid}");
28        $form = $crawler->selectButton('addforum')->form(array(
29            'forum_name'    => 'Prune Shadow',
30        ));
31        $crawler = self::submit($form);
32        $form = $crawler->selectButton('update')->form(array(
33            'forum_perm_from'    => 2,
34            'enable_shadow_prune'    => true,
35            'prune_shadow_freq'    => 1,
36            'prune_shadow_days'    => 1,
37        ));
38        $crawler = self::submit($form);
39    }
40
41    public function test_create_post()
42    {
43        $this->login();
44        $this->load_ids(array(
45            'forums' => array(
46                'Prune Shadow',
47            ),
48        ));
49
50        $this->assert_forum_details($this->data['forums']['Prune Shadow'], array(
51            'forum_posts_approved'        => 0,
52            'forum_posts_unapproved'    => 0,
53            'forum_posts_softdeleted'    => 0,
54            'forum_topics_approved'        => 0,
55            'forum_topics_unapproved'    => 0,
56            'forum_topics_softdeleted'    => 0,
57            'forum_last_post_id'        => 0,
58        ), 'initial comparison');
59
60        // Test creating topic
61        $this->post = $this->create_topic($this->data['forums']['Prune Shadow'], 'Prune Shadow #1', 'This is a test topic posted by the testing framework.');
62        $crawler = self::request('GET', "viewtopic.php?t={$this->post['topic_id']}&sid={$this->sid}");
63
64        $this->assertStringContainsString('Prune Shadow #1', $crawler->filter('html')->text());
65        $this->data['topics']['Prune Shadow #1'] = (int) $this->post['topic_id'];
66        $this->data['posts']['Prune Shadow #1'] = (int) $this->get_parameter_from_link($crawler->filter('.post')->selectLink($this->lang('POST', '', ''))->link()->getUri(), 'p');
67
68        $this->assert_forum_details($this->data['forums']['Prune Shadow'], array(
69            'forum_posts_approved'        => 1,
70            'forum_posts_unapproved'    => 0,
71            'forum_posts_softdeleted'    => 0,
72            'forum_topics_approved'        => 1,
73            'forum_topics_unapproved'    => 0,
74            'forum_topics_softdeleted'    => 0,
75            'forum_last_post_id'        => $this->data['posts']['Prune Shadow #1'],
76        ), 'after creating topic #1');
77
78        // Test creating a reply
79        $post2 = $this->create_post($this->data['forums']['Prune Shadow'], $this->post['topic_id'], 'Re: Prune Shadow #1-#2', 'This is a test post posted by the testing framework.');
80        $crawler = self::request('GET', "viewtopic.php?p={$post2['post_id']}&sid={$this->sid}");
81
82        $this->assertStringContainsString('Re: Prune Shadow #1-#2', $crawler->filter('html')->text());
83        $this->data['posts']['Re: Prune Shadow #1-#2'] = (int) $post2['post_id'];
84
85        $this->assert_forum_details($this->data['forums']['Prune Shadow'], array(
86            'forum_posts_approved'        => 2,
87            'forum_posts_unapproved'    => 0,
88            'forum_posts_softdeleted'    => 0,
89            'forum_topics_approved'        => 1,
90            'forum_topics_unapproved'    => 0,
91            'forum_topics_softdeleted'    => 0,
92            'forum_last_post_id'        => $this->data['posts']['Re: Prune Shadow #1-#2'],
93        ), 'after replying');
94    }
95
96    public function test_move_topic()
97    {
98        $this->login();
99        $this->load_ids(array(
100            'forums' => array(
101                'Prune Shadow',
102            ),
103            'topics' => array(
104                'Prune Shadow #1',
105            ),
106        ));
107
108        $crawler = self::request('GET', "mcp.php?f={$this->data['forums']['Prune Shadow']}&i=main&action=move&mode=forum_view&start=0&topic_id_list[]={$this->data['topics']['Prune Shadow #1']}&sid={$this->sid}");
109        $form = $crawler->selectButton('confirm')->form(array(
110            'to_forum_id'    => 2,
111            'move_leave_shadow' => true,
112        ));
113        $crawler = self::submit($form);
114
115        $this->assert_forum_details($this->data['forums']['Prune Shadow'], array(
116            'forum_posts_approved'        => 0,
117            'forum_posts_unapproved'    => 0,
118            'forum_posts_softdeleted'    => 0,
119            'forum_topics_approved'        => 1,
120            'forum_topics_unapproved'    => 0,
121            'forum_topics_softdeleted'    => 0,
122        ), 'after moving');
123
124        // Date topic 3 days back
125        $sql = 'UPDATE phpbb_topics
126            SET topic_last_post_time = ' . (time() - 60*60*24*3) . '
127            WHERE topic_id = ' . ($this->data['topics']['Prune Shadow #1'] + 1);
128        $this->db->sql_query($sql);
129
130        $crawler = self::request('GET', "viewforum.php?f={$this->data['forums']['Prune Shadow']}&sid={$this->sid}");
131        $this->assertNotEmpty($crawler->filter('img')->last()->attr('src'));
132        self::request('GET', "app.php/cron/cron.task.core.prune_shadow_topics?f={$this->data['forums']['Prune Shadow']}&sid={$this->sid}", array(), false);
133
134        // Try to ensure that the cron can actually run before we start to wait for it
135        usleep(100000);
136        $cron_lock = new \phpbb\lock\db('cron_lock', new \phpbb\config\db($this->db, new \phpbb\cache\driver\dummy(), 'phpbb_config'), $this->db);
137        while (!$cron_lock->acquire())
138        {
139            // do nothing
140        }
141         $cron_lock->release();
142
143        $this->assert_forum_details($this->data['forums']['Prune Shadow'], array(
144            'forum_posts_approved'        => 0,
145            'forum_posts_unapproved'    => 0,
146            'forum_posts_softdeleted'    => 0,
147            'forum_topics_approved'        => 0,
148            'forum_topics_unapproved'    => 0,
149            'forum_topics_softdeleted'    => 0,
150        ), 'after the cron job');
151    }
152
153    public function assert_forum_details($forum_id, $details, $additional_error_message = '')
154    {
155        $sql = 'SELECT ' . implode(', ', array_keys($details)) . '
156            FROM phpbb_forums
157            WHERE forum_id = ' . (int) $forum_id;
158        $result = $this->db->sql_query($sql);
159        $data = $this->db->sql_fetchrow($result);
160        $this->db->sql_freeresult($result);
161
162        $this->assertEquals($details, $data, "Forum {$forum_id} does not match expected {$additional_error_message}");
163    }
164
165    public function load_ids($data)
166    {
167        if (!empty($data['forums']))
168        {
169            $sql = 'SELECT *
170                FROM phpbb_forums
171                WHERE ' . $this->db->sql_in_set('forum_name', $data['forums']);
172            $result = $this->db->sql_query($sql);
173            while ($row = $this->db->sql_fetchrow($result))
174            {
175                if (in_array($row['forum_name'], $data['forums']))
176                {
177                    $this->data['forums'][$row['forum_name']] = (int) $row['forum_id'];
178                }
179            }
180            $this->db->sql_freeresult($result);
181        }
182
183        if (!empty($data['topics']))
184        {
185            $sql = 'SELECT *
186                FROM phpbb_topics
187                WHERE ' . $this->db->sql_in_set('topic_title', $data['topics']);
188            $result = $this->db->sql_query($sql);
189            while ($row = $this->db->sql_fetchrow($result))
190            {
191                if (in_array($row['topic_title'], $data['topics']))
192                {
193                    $this->data['topics'][$row['topic_title']] = (int) $row['topic_id'];
194                }
195            }
196            $this->db->sql_freeresult($result);
197        }
198
199        if (!empty($data['posts']))
200        {
201            $sql = 'SELECT *
202                FROM phpbb_posts
203                WHERE ' . $this->db->sql_in_set('post_subject', $data['posts']);
204            $result = $this->db->sql_query($sql);
205            while ($row = $this->db->sql_fetchrow($result))
206            {
207                if (in_array($row['post_subject'], $data['posts']))
208                {
209                    $this->data['posts'][$row['post_subject']] = (int) $row['post_id'];
210                }
211            }
212            $this->db->sql_freeresult($result);
213        }
214    }
215}