Skip to content

Commit 532b7a6

Browse files
committed
Merge branch 'dev/tests'
Conflicts: controller/main.php
2 parents 35af3dd + 30fa960 commit 532b7a6

8 files changed

Lines changed: 197 additions & 40 deletions

File tree

composer.lock

Lines changed: 22 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.phar

5.55 KB
Binary file not shown.

config/services.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ services:
2424
- @phpbbde.pastebin.functions.pastebin
2525
- %core.root_path%
2626
- %core.php_ext%
27+
- %phpbbde.pastebin.geshi%
28+
- %phpbbde.pastebin.geshilangs%
29+
- %tables.phpbbde.pastebin.pastebin%
2730
phpbbde.pastebin.header_listener:
2831
class: phpbbde\pastebin\event\header_events
2932
arguments:
@@ -49,6 +52,8 @@ services:
4952
- %phpbbde.pastebin.path%
5053
- %core.root_path%
5154
- %core.php_ext%
55+
- %phpbbde.pastebin.cron.prune_interval%
56+
- %tables.phpbbde.pastebin.pastebin%
5257
calls:
5358
- [set_name, [phpbbde.pastebin.cron.main]]
5459
tags:

controller/main.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ class main
5555
/** @var \phpbb\captcha\factory */
5656
protected $captcha_factory;
5757

58-
/** @var string */
59-
protected $ext_path;
60-
6158
/** @var string */
6259
protected $geshi_path;
6360

6461
/** @var string */
6562
protected $geshi_lang;
6663

64+
/** @var string */
65+
protected $pastebin_table;
66+
6767
/**
6868
* Construct
6969
*
@@ -78,7 +78,7 @@ class main
7878
* @param string $root_path
7979
* @param string $php_ext
8080
*/
81-
public function __construct(\phpbb\auth\auth $auth, \phpbb\cache\service $cache, \phpbb\config\config $config, \phpbb\request\request $request, \phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, \phpbb\controller\helper $helper, \phpbb\captcha\factory $captcha_factory, \phpbbde\pastebin\functions\pastebin $pastebin, $root_path, $php_ext)
81+
public function __construct(\phpbb\auth\auth $auth, \phpbb\cache\service $cache, \phpbb\config\config $config, \phpbb\request\request $request, \phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, \phpbb\controller\helper $helper, \phpbb\captcha\factory $captcha_factory, \phpbbde\pastebin\functions\pastebin $pastebin, $root_path, $php_ext, $geshi_path, $geshi_lang, $pastebin_table)
8282
{
8383
$this->auth = $auth;
8484
$this->cache = $cache;
@@ -93,10 +93,9 @@ public function __construct(\phpbb\auth\auth $auth, \phpbb\cache\service $cache,
9393
$this->pastebin = $pastebin;
9494
$this->captcha_factory = $captcha_factory;
9595

96-
global $phpbb_container;
97-
$this->geshi_path = $phpbb_container->getParameter('phpbbde.pastebin.geshi');
98-
$this->ext_path = $phpbb_container->getParameter('phpbbde.pastebin.path');
99-
$this->geshi_lang = $phpbb_container->getParameter('phpbbde.pastebin.geshilangs');
96+
$this->geshi_path = $geshi_path;
97+
$this->pastebin_table = $pastebin_table;
98+
$this->geshi_lang = $geshi_lang;
10099
}
101100

102101
/**
@@ -125,8 +124,10 @@ public function handle($name = '')
125124
*/
126125
private function table($name)
127126
{
128-
global $phpbb_container;
129-
return $phpbb_container->getParameter('tables.phpbbde.pastebin.' . $name);
127+
if($name == 'pastebin')
128+
{
129+
return $this->pastebin_table;
130+
}
130131
}
131132

132133
/**

cron/main.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ class main extends \phpbb\cron\task\base
3030
/** @var \phpbb\log\log_interface */
3131
protected $log;
3232

33-
public function __construct(\phpbb\cache\service $cache, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\log\log_interface $log, $pastebin_path, $root_path, $php_ext)
33+
protected $prune_interval;
34+
protected $pastebin_table;
35+
36+
public function __construct(\phpbb\cache\service $cache, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\log\log_interface $log, $pastebin_path, $root_path, $php_ext, $prune_interval, $pastebin_table)
3437
{
3538
$this->cache = $cache;
3639
$this->config = $config;
@@ -39,6 +42,8 @@ public function __construct(\phpbb\cache\service $cache, \phpbb\config\config $c
3942
$this->pastebin_path = $pastebin_path;
4043
$this->root_path = $root_path;
4144
$this->php_ext = $php_ext;
45+
$this->prune_interval = $prune_interval;
46+
$this->pastebin_table = $pastebin_table;
4247
}
4348

4449
/**
@@ -71,11 +76,9 @@ public function is_runnable()
7176
*/
7277
public function should_run()
7378
{
74-
global $phpbb_container;
75-
7679
$now = time();
7780

78-
return $now > $this->config['phpbbde_pastebin_prune_last_run'] + $phpbb_container->getParameter('phpbbde.pastebin.cron.prune_interval');
81+
return $now > $this->config['phpbbde_pastebin_prune_last_run'] + $this->prune_interval;
7982
}
8083

8184
/**
@@ -85,7 +88,6 @@ public function should_run()
8588
*/
8689
private function table($name)
8790
{
88-
global $phpbb_container;
89-
return $phpbb_container->getParameter('tables.phpbbde.pastebin.' . $name);
91+
return $this->pastebin_table;
9092
}
9193
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<dataset>
3+
<table name="phpbb_pastebin">
4+
<column>snippet_id</column>
5+
<column>snippet_author</column>
6+
<column>snippet_time</column>
7+
<column>snippet_prune_on</column>
8+
<column>snippet_title</column>
9+
<column>snippet_desc</column>
10+
<column>snippet_text</column>
11+
<column>snippet_prunable</column>
12+
<column>snippet_highlight</column>
13+
<row>
14+
<value>1</value>
15+
<value>2</value>
16+
<value>1420396503</value>
17+
<value>2147483646</value>
18+
<value>Should not prune</value>
19+
<value>This entry should not be pruned anytime soon (pruning active but date in the far future).</value>
20+
<value><![CDATA[<?php
21+
echo "Do not prune me yet.";]]></value>
22+
<value>1</value>
23+
<value>php</value>
24+
</row>
25+
<row>
26+
<value>2</value>
27+
<value>2</value>
28+
<value>1420396503</value>
29+
<value>1000000000</value>
30+
<value>Should prune</value>
31+
<value>This entry should be pruned (date in the past and pruning active).</value>
32+
<value><![CDATA[<?php
33+
echo "Please prune me!";]]></value>
34+
<value>1</value>
35+
<value>php</value>
36+
</row>
37+
<row>
38+
<value>3</value>
39+
<value>2</value>
40+
<value>1420396503</value>
41+
<value>1000000000</value>
42+
<value>Should prune</value>
43+
<value>This entry should never be pruned (pruning disabled).</value>
44+
<value><![CDATA[<?php
45+
echo "Please never prune me!";]]></value>
46+
<value>0</value>
47+
<value>php</value>
48+
</row>
49+
</table>
50+
<table name="phpbb_log">
51+
<column>log_id</column>
52+
<column>log_type</column>
53+
<column>user_id</column>
54+
<column>forum_id</column>
55+
<column>topic_id</column>
56+
<column>reportee_id</column>
57+
<column>log_ip</column>
58+
<column>log_time</column>
59+
<column>log_operation</column>
60+
<column>log_data</column>
61+
</table>
62+
</dataset>

tests/cron/main_test.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/**
4+
*
5+
* @package testing
6+
* @copyright (c) 2015 phpBB.de, gn#36
7+
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8+
*
9+
*/
10+
11+
class phpbbde_cron_main_test extends phpbb_database_test_case
12+
{
13+
static protected function setup_extensions()
14+
{
15+
return array('phpbbde/pastebin');
16+
}
17+
18+
public function getDataSet()
19+
{
20+
return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/three_pastebin_entries.xml');
21+
}
22+
23+
public function setUp()
24+
{
25+
parent::setUp();
26+
27+
$this->cache = $this->getMockBuilder('\phpbb\cache\service')->disableOriginalConstructor()->getMock();
28+
$this->log = $this->getMockBuilder('\phpbb\log\log')->disableOriginalConstructor()->getMock();
29+
}
30+
31+
public function test_construct()
32+
{
33+
$task = $this->get_task();
34+
$this->assertInstanceOf('\phpbb\cron\task\base', $task);
35+
}
36+
37+
public function test_is_runnable()
38+
{
39+
$task = $this->get_task();
40+
$this->assertTrue($task->is_runnable());
41+
}
42+
43+
public function test_should_run()
44+
{
45+
// 1: Has not run ever
46+
$task = $this->get_task();
47+
$this->assertTrue($task->should_run());
48+
49+
// 2: Has just run
50+
$task = $this->get_task(time() - 1);
51+
$this->assertTrue(!$task->should_run());
52+
}
53+
54+
public function test_run()
55+
{
56+
$task = $this->get_task();
57+
$task->run();
58+
$sql = 'SELECT count(*) as cnt FROM phpbb_pastebin';
59+
60+
$result = $this->db->sql_query($sql);
61+
$row = $this->db->sql_fetchrow($result);
62+
$this->db->sql_freeresult($result);
63+
$this->assertEquals($row['cnt'], 2);
64+
65+
$sql = 'SELECT snippet_id FROM phpbb_pastebin';
66+
$result = $this->db->sql_query($sql);
67+
$rows = $this->db->sql_fetchrowset($result);
68+
$this->db->sql_freeresult($result);
69+
$this->assertEquals($rows, array(array('snippet_id' => 1), array('snippet_id' => 3)));
70+
}
71+
72+
private function get_task($last_run = 0)
73+
{
74+
global $phpbb_root_path, $phpEx;
75+
$pastebin_path = dirname(__FILE__) . '/../../';
76+
$db = $this->new_dbal();
77+
$this->db = $db;
78+
79+
$this->config = new \phpbb\config\config(array(
80+
'phpbbde_pastebin_prune_last_run' => $last_run,
81+
'phpbbde_pastebin_version' => '0.2.2',
82+
));
83+
84+
return new \phpbbde\pastebin\cron\main($this->cache, $this->config, $db, $this->log, $pastebin_path, $phpbb_root_path, $phpEx, 86400, 'phpbb_pastebin');
85+
}
86+
}

0 commit comments

Comments
 (0)