Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
58.14% covered (warning)
58.14%
25 / 43
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_attachment_delete_test
58.14% covered (warning)
58.14%
25 / 43
50.00% covered (danger)
50.00%
3 / 6
12.69
0.00% covered (danger)
0.00%
0 / 1
 getDataSet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setUp
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 data_attachment_delete
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 test_attachment_delete
50.00% covered (danger)
50.00%
2 / 4
0.00% covered (danger)
0.00%
0 / 1
2.50
 data_attachment_unlink
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 test_attachment_delete_success
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 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
14require_once(__DIR__ . '/../../phpBB/includes/functions_admin.php');
15
16class phpbb_attachment_delete_test extends \phpbb_database_test_case
17{
18    /** @var \phpbb\config\config */
19    protected $config;
20
21    /** @var \phpbb\db\driver\driver_interface */
22    protected $db;
23
24    /** @var \phpbb\event\dispatcher_interface */
25    protected $dispatcher;
26
27    /** @var \phpbb\attachment\resync */
28    protected $resync;
29
30    /** @var \phpbb\storage\storage */
31    protected $storage;
32
33    /** @var \phpbb\attachment\delete */
34    protected $attachment_delete;
35
36    public function getDataSet()
37    {
38        return $this->createXMLDataSet(__DIR__ . '/fixtures/resync.xml');
39    }
40
41    protected function setUp(): void
42    {
43        parent::setUp();
44
45        $this->config = new \phpbb\config\config(array());
46        $this->db = $this->new_dbal();
47        $this->resync = new \phpbb\attachment\resync($this->db);
48        $this->storage = $this->createMock('\phpbb\storage\storage');
49        $this->storage->expects($this->any())
50            ->method('exists')
51            ->willReturn(true);
52        $this->dispatcher = new \phpbb_mock_event_dispatcher();
53        $this->attachment_delete = new \phpbb\attachment\delete($this->config, $this->db, $this->dispatcher, $this->resync, $this->storage);
54    }
55
56    public static function data_attachment_delete()
57    {
58        return array(
59            array('attach', '', false, false),
60            array('meh', 5, false, 0),
61            array('attach', array(5), false, 0),
62            array('attach', array(1,2), false, 2),
63            array('attach', array(1,2), true, 2),
64            array('post', 5, false, 0),
65            array('topic', 5, false, 0),
66            array('topic', 1, true, 3),
67            array('user', 1, false, 0),
68        );
69    }
70
71    /**
72     * @dataProvider data_attachment_delete
73     */
74    public function test_attachment_delete($mode, $ids, $resync, $expected)
75    {
76        // We need to reset the attachment ID sequence to properly test this
77        if ($this->db->get_sql_layer() === 'postgres')
78        {
79            $sql = 'ALTER SEQUENCE phpbb_attachments_seq RESTART WITH 1';
80            $this->db->sql_query($sql);
81        }
82
83        $this->assertSame($expected, $this->attachment_delete->delete($mode, $ids, $resync));
84    }
85
86    public static function data_attachment_unlink()
87    {
88        return array(
89            array(true, true),
90            array(false, false),
91            array(true, false, true),
92        );
93    }
94
95    /**
96     * @dataProvider data_attachment_unlink
97     */
98    public function test_attachment_delete_success($exists_success, $expected, $throw_exception = false)
99    {
100        $this->storage = $this->createMock('\phpbb\storage\storage');
101        if ($throw_exception)
102        {
103            $this->storage->expects($this->any())
104                ->method('delete')
105                ->willThrowException(new \phpbb\storage\exception\storage_exception);
106        }
107        else
108        {
109            $this->storage->expects($this->any())
110                ->method('delete');
111        }
112        $this->storage->expects($this->any())
113            ->method('exists')
114            ->willReturn($exists_success);
115
116        $this->attachment_delete = new \phpbb\attachment\delete($this->config, $this->db, $this->dispatcher, $this->resync, $this->storage);
117        $this->assertSame($expected, $this->attachment_delete->unlink_attachment('foobar'));
118    }
119}