aboutsummaryrefslogtreecommitdiff
blob: f1835dd37a27505d85e3909491227a05f52b8726 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

require_once(dirname(__FILE__) . '/../../phpBB/includes/functions_admin.php');

class phpbb_attachment_delete_test extends \phpbb_database_test_case
{
	/** @var \phpbb\config\config */
	protected $config;

	/** @var \phpbb\db\driver\driver_interface */
	protected $db;

	/** @var \phpbb\filesystem\filesystem */
	protected $filesystem;

	/** @var \phpbb\attachment\resync */
	protected $resync;

	/** @var \phpbb\attachment\delete */
	protected $attachment_delete;

	protected $phpbb_root_path;

	public function getDataSet()
	{
		return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/resync.xml');
	}

	public function setUp()
	{
		global $db, $phpbb_root_path;

		parent::setUp();

		$this->config = new \phpbb\config\config(array());
		$this->db = $this->new_dbal();
		$db = $this->db;
		$this->resync = new \phpbb\attachment\resync($this->db);
		$this->filesystem = $this->getMock('\phpbb\filesystem\filesystem', array('remove', 'exists'));
		$this->filesystem->expects($this->any())
			->method('remove')
			->willReturn(false);
		$this->filesystem->expects($this->any())
			->method('exists')
			->willReturn(true);
		$this->phpbb_root_path = $phpbb_root_path;
		$this->dispatcher = new \phpbb_mock_event_dispatcher();
		$this->attachment_delete = new \phpbb\attachment\delete($this->config, $this->db, $this->dispatcher, $this->filesystem, $this->resync, $phpbb_root_path);
	}

	public function data_attachment_delete()
	{
		return array(
			array('attach', '', false, false),
			array('meh', 5, false, 0),
			array('attach', array(5), false, 0),
			array('attach', array(1,2), false, 2),
			array('attach', array(1,2), true, 2),
			array('post', 5, false, 0),
			array('topic', 5, false, 0),
			array('topic', 1, true, 3),
			array('user', 1, false, 0),
		);
	}

	/**
	 * @dataProvider data_attachment_delete
	 */
	public function test_attachment_delete($mode, $ids, $resync, $expected)
	{
		// We need to reset the attachment ID sequence to properly test this
		if ($this->db->get_sql_layer() === 'postgres')
		{
			$sql = 'ALTER SEQUENCE phpbb_attachments_seq RESTART WITH 1';
			$this->db->sql_query($sql);
		}

		$this->assertSame($expected, $this->attachment_delete->delete($mode, $ids, $resync));
	}

	public function data_attachment_unlink()
	{
		return array(
			array(true, true, true),
			array(true, false, false),
			array(true, true, false, true),
		);
	}

	/**
	 * @dataProvider data_attachment_unlink
	 */
	public function test_attachment_delete_success($remove_success, $exists_success, $expected, $throw_exception = false)
	{
		$this->filesystem = $this->getMock('\phpbb\filesystem\filesystem', array('remove', 'exists'));
		if ($throw_exception)
		{
			$this->filesystem->expects($this->any())
				->method('remove')
				->willThrowException(new \phpbb\filesystem\exception\filesystem_exception);;
		}
		else
		{
			$this->filesystem->expects($this->any())
				->method('remove')
				->willReturn($remove_success);
		}

		$this->filesystem->expects($this->any())
			->method('exists')
			->willReturn($exists_success);

		$this->attachment_delete = new \phpbb\attachment\delete($this->config, $this->db, $this->dispatcher, $this->filesystem, $this->resync, $this->phpbb_root_path);
		$this->assertSame($expected, $this->attachment_delete->unlink_attachment('foobar'));
	}
}