summaryrefslogtreecommitdiffstats
path: root/tests/lib/Files/Cache/WatcherTest.php
blob: 42115c1c477042c4d80403cf0fcb2f9f963e6ffd (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
 * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

namespace Test\Files\Cache;

/**
 * Class WatcherTest
 *
 * @group DB
 *
 * @package Test\Files\Cache
 */
class WatcherTest extends \Test\TestCase {

	/**
	 * @var \OC\Files\Storage\Storage[] $storages
	 */
	private $storages = [];

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

		$this->loginAsUser();
	}

	protected function tearDown(): void {
		foreach ($this->storages as $storage) {
			$cache = $storage->getCache();
			$ids = $cache->getAll();
			$cache->clear();
		}

		$this->logout();
		parent::tearDown();
	}

	/**
	 * @medium
	 */
	public function testWatcher() {
		$storage = $this->getTestStorage();
		$cache = $storage->getCache();
		$updater = $storage->getWatcher();
		$updater->setPolicy(\OC\Files\Cache\Watcher::CHECK_ONCE);

		//set the mtime to the past so it can detect an mtime change
		$cache->put('', ['storage_mtime' => 10]);

		$this->assertTrue($cache->inCache('folder/bar.txt'));
		$this->assertTrue($cache->inCache('folder/bar2.txt'));

		$this->assertFalse($cache->inCache('bar.test'));
		$storage->file_put_contents('bar.test', 'foo');
		$updater->checkUpdate('');
		$this->assertTrue($cache->inCache('bar.test'));
		$cachedData = $cache->get('bar.test');
		$this->assertEquals(3, $cachedData['size']);

		$cache->put('bar.test', ['storage_mtime' => 10]);
		$storage->file_put_contents('bar.test', 'test data');

		// make sure that PHP can read the new size correctly
		clearstatcache();

		$updater->checkUpdate('bar.test');
		$cachedData = $cache->get('bar.test');
		$this->assertEquals(9, $cachedData['size']);

		$cache->put('folder', ['storage_mtime' => 10]);

		$storage->unlink('folder/bar2.txt');
		$updater->checkUpdate('folder');

		$this->assertTrue($cache->inCache('folder/bar.txt'));
		$this->assertFalse($cache->inCache('folder/bar2.txt'));
	}

	/**
	 * @medium
	 */
	public function testFileToFolder() {
		$storage = $this->getTestStorage();
		$cache = $storage->getCache();
		$updater = $storage->getWatcher();
		$updater->setPolicy(\OC\Files\Cache\Watcher::CHECK_ONCE);

		//set the mtime to the past so it can detect an mtime change
		$cache->put('', ['storage_mtime' => 10]);

		$storage->unlink('foo.txt');
		$storage->rename('folder', 'foo.txt');
		$updater->checkUpdate('');

		$entry = $cache->get('foo.txt');
		$this->assertEquals('httpd/unix-directory', $entry['mimetype']);
		$this->assertFalse($cache->inCache('folder'));
		$this->assertFalse($cache->inCache('folder/bar.txt'));

		$storage = $this->getTestStorage();
		$cache = $storage->getCache();
		$updater = $storage->getWatcher();
		$updater->setPolicy(\OC\Files\Cache\Watcher::CHECK_ONCE);

		//set the mtime to the past so it can detect an mtime change
		$cache->put('foo.txt', ['storage_mtime' => 10]);

		$storage->unlink('foo.txt');
		$storage->rename('folder', 'foo.txt');
		$updater->checkUpdate('foo.txt');

		$entry = $cache->get('foo.txt');
		$this->assertEquals('httpd/unix-directory', $entry['mimetype']);
		$this->assertTrue($cache->inCache('foo.txt/bar.txt'));
	}

	public function testPolicyNever() {
		$storage = $this->getTestStorage();
		$cache = $storage->getCache();
		$updater = $storage->getWatcher();

		//set the mtime to the past so it can detect an mtime change
		$cache->put('foo.txt', ['storage_mtime' => 10]);

		$updater->setPolicy(\OC\Files\Cache\Watcher::CHECK_NEVER);

		$storage->file_put_contents('foo.txt', 'q');
		$this->assertFalse($updater->checkUpdate('foo.txt'));

		$cache->put('foo.txt', ['storage_mtime' => 20]);
		$storage->file_put_contents('foo.txt', 'w');
		$this->assertFalse($updater->checkUpdate('foo.txt'));
	}

	public function testPolicyOnce() {
		$storage = $this->getTestStorage();
		$cache = $storage->getCache();
		$updater = $storage->getWatcher();

		//set the mtime to the past so it can detect an mtime change
		$cache->put('foo.txt', ['storage_mtime' => 10]);

		$updater->setPolicy(\OC\Files\Cache\Watcher::CHECK_ONCE);

		$storage->file_put_contents('foo.txt', 'q');
		$this->assertTrue($updater->checkUpdate('foo.txt'));

		$cache->put('foo.txt', ['storage_mtime' => 20]);
		$storage->file_put_contents('foo.txt', 'w');
		$this->assertFalse($updater->checkUpdate('foo.txt'));
	}

	public function testPolicyAlways() {
		$storage = $this->getTestStorage();
		$cache = $storage->getCache();
		$updater = $storage->getWatcher();

		//set the mtime to the past so it can detect an mtime change
		$cache->put('foo.txt', ['storage_mtime' => 10]);

		$updater->setPolicy(\OC\Files\Cache\Watcher::CHECK_ALWAYS);

		$storage->file_put_contents('foo.txt', 'q');
		$this->assertTrue($updater->checkUpdate('foo.txt'));

		$cache->put('foo.txt', ['storage_mtime' => 20]);
		$storage->file_put_contents('foo.txt', 'w');
		$this->assertTrue($updater->checkUpdate('foo.txt'));
	}

	/**
	 * @param bool $scan
	 * @return \OC\Files\Storage\Storage
	 */
	private function getTestStorage($scan = true) {
		$storage = new \OC\Files\Storage\Temporary([]);
		$textData = "dummy file data\n";
		$imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo/logo.png');
		$storage->mkdir('folder');
		$storage->file_put_contents('foo.txt', $textData);
		$storage->file_put_contents('foo.png', $imgData);
		$storage->file_put_contents('folder/bar.txt', $textData);
		$storage->file_put_contents('folder/bar2.txt', $textData);

		if ($scan) {
			$scanner = $storage->getScanner();
			$scanner->scan('');
		}
		$this->storages[] = $storage;
		return $storage;
	}
}