|
| 1 | +<?php |
| 2 | +/** |
| 3 | +* |
| 4 | +* @copyright (c) Derky <http://www.derky.nl> |
| 5 | +* @license GNU General Public License, version 2 (GPL-2.0) |
| 6 | +* |
| 7 | +*/ |
| 8 | + |
| 9 | +namespace derky\sortablescaptcha\captcha\tests\feature; |
| 10 | + |
| 11 | +class garbage_collection_test extends \phpbb_database_test_case |
| 12 | +{ |
| 13 | + /** @var \phpbb\db\tools */ |
| 14 | + protected $db_tools; |
| 15 | + |
| 16 | + /** @var string */ |
| 17 | + protected $table_prefix; |
| 18 | + |
| 19 | + /** @var \phpbb\db\driver\driver_interface */ |
| 20 | + private $db; |
| 21 | + |
| 22 | + static protected function setup_extensions() |
| 23 | + { |
| 24 | + return array('derky/sortablescaptcha'); |
| 25 | + } |
| 26 | + |
| 27 | + public function getDataSet() |
| 28 | + { |
| 29 | + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/garbage_collection.xml'); |
| 30 | + } |
| 31 | + |
| 32 | + public function setUp() |
| 33 | + { |
| 34 | + parent::setUp(); |
| 35 | + |
| 36 | + global $table_prefix; |
| 37 | + |
| 38 | + $this->table_prefix = $table_prefix; |
| 39 | + $this->db = $this->new_dbal(); |
| 40 | + $this->db_tools = new \phpbb\db\tools($this->db); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Construct sortables class with mocked services except database driver |
| 45 | + * |
| 46 | + * @param array $input_post_array Variables like submitted with $_POST |
| 47 | + * @return \derky\sortablescaptcha\captcha\sortables |
| 48 | + */ |
| 49 | + public function get_sortables($input_post_array = array()) |
| 50 | + { |
| 51 | + global $phpbb_root_path, $phpEx; |
| 52 | + |
| 53 | + $request = new \phpbb_mock_request(array(), $input_post_array); |
| 54 | + |
| 55 | + $this->cache = $this->getMockBuilder('\phpbb\cache\driver\driver_interface') |
| 56 | + ->disableOriginalConstructor() |
| 57 | + ->getMock(); |
| 58 | + |
| 59 | + $this->config = new \phpbb\config\config(array()); |
| 60 | + |
| 61 | + $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); |
| 62 | + |
| 63 | + $this->user = $this->getMockBuilder('\phpbb\user') |
| 64 | + ->setConstructorArgs(array( |
| 65 | + $this->language, |
| 66 | + '\phpbb\datetime' |
| 67 | + )) |
| 68 | + ->getMock(); |
| 69 | + |
| 70 | + $this->template = $this->getMockBuilder('\phpbb\template\template') |
| 71 | + ->disableOriginalConstructor() |
| 72 | + ->getMock(); |
| 73 | + |
| 74 | + return new \derky\sortablescaptcha\captcha\sortables( |
| 75 | + $this->db, |
| 76 | + $this->cache, |
| 77 | + new \phpbb\config\config(array()), |
| 78 | + new \phpbb\log\dummy(), |
| 79 | + $request, |
| 80 | + $this->template, |
| 81 | + $this->user, |
| 82 | + 'phpbb_sortables_questions', |
| 83 | + 'phpbb_sortables_answers', |
| 84 | + 'phpbb_sortables_confirm'); |
| 85 | + } |
| 86 | + |
| 87 | + public function test_sortables_confirm_table_exists() |
| 88 | + { |
| 89 | + $this->assertTrue($this->db_tools->sql_table_exists($this->table_prefix . 'sortables_confirm'), 'Asserting that table "' . $this->table_prefix . 'sortables_confirm" exist'); |
| 90 | + } |
| 91 | + |
| 92 | + public function test_sortables_garbage_collection() |
| 93 | + { |
| 94 | + // Garbage collection should delete 2 from the 3 rows because related sessions no longer exists |
| 95 | + $expected_confirm_rows = 1; |
| 96 | + $sortables = $this->get_sortables(); |
| 97 | + $sortables->garbage_collect(); |
| 98 | + |
| 99 | + $sql = 'SELECT COUNT(confirm_id) AS count |
| 100 | + FROM ' . $this->table_prefix . 'sortables_confirm'; |
| 101 | + $this->db->sql_query($sql); |
| 102 | + $count = (int) $this->db->sql_fetchfield('count'); |
| 103 | + |
| 104 | + $this->assertEquals($expected_confirm_rows, $count); |
| 105 | + } |
| 106 | +} |
0 commit comments