Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 73 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| phpbb_console_command_thumbnail_test | |
0.00% |
0 / 73 |
|
0.00% |
0 / 5 |
72 | |
0.00% |
0 / 1 |
| getDataSet | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setUp | |
0.00% |
0 / 41 |
|
0.00% |
0 / 1 |
6 | |||
| tearDown | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
| test_thumbnails | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
2 | |||
| get_command_tester | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 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 | use phpbb\attachment\attachment_category; |
| 15 | use Symfony\Component\Console\Application; |
| 16 | use Symfony\Component\Console\Tester\CommandTester; |
| 17 | use phpbb\console\command\thumbnail\generate; |
| 18 | use phpbb\console\command\thumbnail\delete; |
| 19 | use phpbb\console\command\thumbnail\recreate; |
| 20 | |
| 21 | class phpbb_console_command_thumbnail_test extends phpbb_database_test_case |
| 22 | { |
| 23 | protected $db; |
| 24 | protected $config; |
| 25 | protected $cache; |
| 26 | protected $language; |
| 27 | protected $user; |
| 28 | protected $storage; |
| 29 | protected $temp; |
| 30 | protected $phpEx; |
| 31 | protected $phpbb_root_path; |
| 32 | protected $application; |
| 33 | |
| 34 | public function getDataSet() |
| 35 | { |
| 36 | return $this->createXMLDataSet(__DIR__ . '/fixtures/thumbnail.xml'); |
| 37 | } |
| 38 | |
| 39 | protected function setUp(): void |
| 40 | { |
| 41 | global $config, $phpbb_root_path, $phpEx, $phpbb_filesystem; |
| 42 | |
| 43 | if (!@extension_loaded('gd')) |
| 44 | { |
| 45 | $this->markTestSkipped('Thumbnail tests require gd extension.'); |
| 46 | } |
| 47 | |
| 48 | parent::setUp(); |
| 49 | |
| 50 | $config = $this->config = new \phpbb\config\config(array( |
| 51 | 'img_min_thumb_filesize' => 2, |
| 52 | 'img_max_thumb_width' => 2 |
| 53 | )); |
| 54 | |
| 55 | $this->db = $this->new_dbal(); |
| 56 | $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); |
| 57 | $this->user = new \phpbb\user($this->language, '\phpbb\datetime'); |
| 58 | $this->phpbb_root_path = $phpbb_root_path; |
| 59 | $this->phpEx = $phpEx; |
| 60 | |
| 61 | $this->cache = $this->createMock('\phpbb\cache\service'); |
| 62 | $this->cache->expects(self::any())->method('obtain_attach_extensions')->will(self::returnValue(array( |
| 63 | 'png' => array('display_cat' => attachment_category::IMAGE), |
| 64 | 'txt' => array('display_cat' => attachment_category::NONE), |
| 65 | ))); |
| 66 | |
| 67 | $phpbb_filesystem = new \phpbb\filesystem\filesystem(); |
| 68 | |
| 69 | $this->storage = $this->createMock('\phpbb\storage\storage'); |
| 70 | $this->storage->method('write')->willReturnCallback(function ($path, $data) use ($phpbb_root_path) { |
| 71 | file_put_contents($phpbb_root_path . 'files/' . $path, $data); |
| 72 | }); |
| 73 | $this->storage->method('read')->willReturnCallback(function ($path) use ($phpbb_root_path) { |
| 74 | return fopen($phpbb_root_path . 'files/' . $path, 'rb'); |
| 75 | }); |
| 76 | $this->storage->method('delete')->willReturnCallback(function ($path) use ($phpbb_root_path) { |
| 77 | unlink($phpbb_root_path . 'files/' . $path); |
| 78 | }); |
| 79 | |
| 80 | $this->temp = $this->createMock('\phpbb\filesystem\temp'); |
| 81 | $this->temp->method('get_dir')->willReturnCallback(function () { |
| 82 | return sys_get_temp_dir(); |
| 83 | }); |
| 84 | |
| 85 | $this->application = new Application(); |
| 86 | $this->application->add(new generate($this->user, $this->db, $this->cache, $this->language, $this->storage, $this->temp, $this->phpbb_root_path, $this->phpEx)); |
| 87 | $this->application->add(new delete($this->user, $this->db, $this->language, $this->storage)); |
| 88 | $this->application->add(new recreate($this->user, $this->language)); |
| 89 | |
| 90 | copy(__DIR__ . '/fixtures/png.png', $this->phpbb_root_path . 'files/test_png_1'); |
| 91 | copy(__DIR__ . '/fixtures/png.png', $this->phpbb_root_path . 'files/test_png_2'); |
| 92 | copy(__DIR__ . '/fixtures/png.png', $this->phpbb_root_path . 'files/thumb_test_png_2'); |
| 93 | copy(__DIR__ . '/fixtures/txt.txt', $this->phpbb_root_path . 'files/test_txt'); |
| 94 | } |
| 95 | |
| 96 | protected function tearDown(): void |
| 97 | { |
| 98 | parent::tearDown(); |
| 99 | |
| 100 | $delete_files = [ |
| 101 | $this->phpbb_root_path . 'files/test_png_1', |
| 102 | $this->phpbb_root_path . 'files/test_png_2', |
| 103 | $this->phpbb_root_path . 'files/test_txt', |
| 104 | $this->phpbb_root_path . 'files/thumb_test_png_1', |
| 105 | $this->phpbb_root_path . 'files/thumb_test_png_2' |
| 106 | ]; |
| 107 | |
| 108 | foreach ($delete_files as $file) |
| 109 | { |
| 110 | if (file_exists($file)) |
| 111 | { |
| 112 | unlink($file); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | public function test_thumbnails() |
| 118 | { |
| 119 | $command_tester = $this->get_command_tester('thumbnail:generate'); |
| 120 | $exit_status = $command_tester->execute([]); |
| 121 | |
| 122 | self::assertSame(true, file_exists($this->phpbb_root_path . 'files/thumb_test_png_1')); |
| 123 | self::assertSame(true, file_exists($this->phpbb_root_path . 'files/thumb_test_png_2')); |
| 124 | self::assertSame(false, file_exists($this->phpbb_root_path . 'files/thumb_test_txt')); |
| 125 | self::assertSame(0, $exit_status); |
| 126 | |
| 127 | $command_tester = $this->get_command_tester('thumbnail:delete'); |
| 128 | $exit_status = $command_tester->execute([]); |
| 129 | |
| 130 | self::assertSame(false, file_exists($this->phpbb_root_path . 'files/thumb_test_png_1')); |
| 131 | self::assertSame(false, file_exists($this->phpbb_root_path . 'files/thumb_test_png_2')); |
| 132 | self::assertSame(false, file_exists($this->phpbb_root_path . 'files/thumb_test_txt')); |
| 133 | self::assertSame(0, $exit_status); |
| 134 | |
| 135 | $command_tester = $this->get_command_tester('thumbnail:recreate'); |
| 136 | $exit_status = $command_tester->execute([]); |
| 137 | |
| 138 | self::assertSame(true, file_exists($this->phpbb_root_path . 'files/thumb_test_png_1')); |
| 139 | self::assertSame(true, file_exists($this->phpbb_root_path . 'files/thumb_test_png_2')); |
| 140 | self::assertSame(false, file_exists($this->phpbb_root_path . 'files/thumb_test_txt')); |
| 141 | self::assertSame(0, $exit_status); |
| 142 | } |
| 143 | |
| 144 | public function get_command_tester($command_name) |
| 145 | { |
| 146 | $command = $this->application->find($command_name); |
| 147 | return new CommandTester($command); |
| 148 | } |
| 149 | } |