aboutsummaryrefslogtreecommitdiff
blob: e44f0e20239fffa6ca1cfa732ccd7e3cd6cf2c3a (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
<?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.
 *
 */

 class phpbb_storage_adapter_local_test extends phpbb_test_case
 {
	protected $adapter;

	protected $path;

	protected $filesystem;

	public function setUp(): void
	{
		parent::setUp();

		$this->filesystem = new \phpbb\filesystem\filesystem();
		$phpbb_root_path = getcwd() . DIRECTORY_SEPARATOR;

		$this->adapter = new \phpbb\storage\adapter\local($this->filesystem, new \FastImageSize\FastImageSize(), new \phpbb\mimetype\guesser(array(new \phpbb\mimetype\extension_guesser)), $phpbb_root_path);
		$this->adapter->configure(['path' => 'test_path', 'subfolders' => false]);

		$this->path = $phpbb_root_path . 'test_path/';
		mkdir($this->path);
	}

	public function tearDown(): void
	{
		$this->adapter = null;
		rmdir($this->path);
	}

	public function test_put_contents()
	{
		$this->adapter->put_contents('file.txt', 'abc');
		$this->assertTrue(file_exists($this->path . 'file.txt'));
		$this->assertEquals(file_get_contents($this->path . 'file.txt'), 'abc');
		unlink($this->path . 'file.txt');
	}

	public function test_get_contents()
	{
		file_put_contents($this->path . 'file.txt', 'abc');
		$this->assertEquals($this->adapter->get_contents('file.txt'), 'abc');
		unlink($this->path . 'file.txt');
	}

	public function test_exists()
	{
		touch($this->path . 'file.txt');
		$this->assertTrue($this->adapter->exists('file.txt'));
		$this->assertFalse($this->adapter->exists('noexist.txt'));
		unlink($this->path . 'file.txt');
	}

	public function test_delete_file()
	{
		touch($this->path . 'file.txt');
		$this->assertTrue(file_exists($this->path . 'file.txt'));
		$this->adapter->delete('file.txt');
		$this->assertFalse(file_exists($this->path . 'file.txt'));
	}

	public function test_rename()
	{
		touch($this->path . 'file.txt');
		$this->adapter->rename('file.txt', 'file2.txt');
		$this->assertFalse(file_exists($this->path . 'file.txt'));
		$this->assertTrue(file_exists($this->path . 'file2.txt'));
		$this->assertFalse(file_exists($this->path . 'file.txt'));
		unlink($this->path . 'file2.txt');
	}

	public function test_copy()
	{
		file_put_contents($this->path . 'file.txt', 'abc');
		$this->adapter->copy('file.txt', 'file2.txt');
		$this->assertEquals(file_get_contents($this->path . 'file.txt'), 'abc');
		$this->assertEquals(file_get_contents($this->path . 'file2.txt'), 'abc');
		unlink($this->path . 'file.txt');
		unlink($this->path . 'file2.txt');
	}

	public function test_read_stream()
	{
		touch($this->path . 'file.txt');
		$stream = $this->adapter->read_stream('file.txt');
		$this->assertTrue(is_resource($stream));
		fclose($stream);
		unlink($this->path . 'file.txt');
	}

	public function test_write_stream()
	{
		file_put_contents($this->path . 'file.txt', 'abc');
		$stream = fopen($this->path . 'file.txt', 'rb');
		$this->adapter->write_stream('file2.txt', $stream);
		fclose($stream);
		$this->assertEquals(file_get_contents($this->path . 'file2.txt'), 'abc');
		unlink($this->path . 'file.txt');
		unlink($this->path . 'file2.txt');
	}

 }