<commit_before><?php
/**
 * Copyright (c) 2012 <NAME> <<EMAIL>>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

namespace Test\Files\Cache;

use OC\Files\Filesystem;
use OC\Files\View;

/**
 * Class UpdaterLegacyTest
 *
 * @group DB
 *
 * @package Test\Files\Cache
 */
class UpdaterLegacyTest extends \Test\TestCase {
	/**
	 * @var \OC\Files\Storage\Storage $storage
	 */
	private $storage;

	/**
	 * @var \OC\Files\Cache\Scanner $scanner
	 */
	private $scanner;

	/**
	 * @var \OC\Files\Cache\Cache $cache
	 */
	private $cache;

	private static $user;

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

		$this->storage = new \OC\Files\Storage\Temporary([]);
		$textData = "dummy file data\n";
		$imgData = \file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
		$this->storage->mkdir('folder');
		$this->storage->file_put_contents('foo.txt', $textData);
		$this->storage->file_put_contents('foo.png', $imgData);
		$this->storage->file_put_contents('folder/bar.txt', $textData);
		$this->storage->file_put_contents('folder/bar2.txt', $textData);

		$this->scanner = $this->storage->getScanner();
		$this->scanner->scan('');
		$this->cache = $this->storage->getCache();

		if (!self::$user) {
			self::$user = $this->getUniqueID();
		}

		\OC::$server->getUserManager()->createUser(self::$user, 'password');
		$this->loginAsUser(self::$user);

		Filesystem::init(self::$user, '/' . self::$user . '/files');

		Filesystem::clearMounts();
		Filesystem::mount($this->storage, [], '/' . self::$user . '/files');

		\OC_Hook::clear('OC_Filesystem');
	}

	protected function tearDown(): void {
		if ($this->cache) {
			$this->cache->clear();
		}

		$result = false;
		$user = \OC::$server->getUserManager()->get(self::$user);
		if ($user !== null) {
			$result = $user->delete();
		}
		$this->assertTrue($result);

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

	public function testWrite() {
		$textSize = \strlen("dummy file data\n");
		$imageSize = \filesize(\OC::$SERVERROOT . '/core/img/logo.png');
		$this->cache->put('foo.txt', ['mtime' => 100, 'storage_mtime' => 150]);
		$rootCachedData = $this->cache->get('');
		$this->assertEquals(3 * $textSize + $imageSize, $rootCachedData['size']);

		$fooCachedData = $this->cache->get('foo.txt');
		Filesystem::file_put_contents('foo.txt', 'asd');
		$cachedData = $this->cache->get('foo.txt');
		$this->assertEquals(3, $cachedData['size']);
		$this->assertIsString($fooCachedData['etag']);
		$this->assertIsString($cachedData['etag']);
		$this->assertNotSame($fooCachedData['etag'], $cachedData['etag']);
		$cachedData = $this->cache->get('');
		$this->assertEquals(2 * $textSize + $imageSize + 3, $cachedData['size']);
		$this->assertIsString($rootCachedData['etag']);
		$this->assertIsString($cachedData['etag']);
		$this->assertNotSame($rootCachedData['etag'], $cachedData['etag']);
		$rootCachedData = $cachedData;

		$this->assertFalse($this->cache->inCache('bar.txt'));
		Filesystem::file_put_contents('bar.txt', 'asd');
		$this->assertTrue($this->cache->inCache('bar.txt'));
		$cachedData = $this->cache->get('bar.txt');
		$this->assertEquals(3, $cachedData['size']);
		$mtime = $cachedData['mtime'];
		$cachedData = $this->cache->get('');
		$this->assertEquals(2 * $textSize + $imageSize + 2 * 3, $cachedData['size']);
		$this->assertIsString($rootCachedData['etag']);
		$this->assertIsString($cachedData['etag']);
		$this->assertNotSame($rootCachedData['etag'], $cachedData['etag']);
		$this->assertGreaterThanOrEqual($rootCachedData['mtime'], $mtime);
	}

	public function testWriteWithMountPoints() {
		$storage2 = new \OC\Files\Storage\Temporary([]);
		$storage2->getScanner()->scan(''); //initialize etags
		$cache2 = $storage2->getCache();
		Filesystem::mount($storage2, [], '/' . self::$user . '/files/folder/substorage');
		$view = new View('/' . self::$user . '/files');
		$folderCachedData = $view->getFileInfo('folder');
		$substorageCachedData = $cache2->get('');<commit_msg>Split UpdaterLegacyTest overwrite and write-new-file unit tests<commit_after><?php
/**
 * Copyright (c) 2012 <NAME> <<EMAIL>>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

namespace Test\Files\Cache;

use OC\Files\Filesystem;
use OC\Files\View;

/**
 * Class UpdaterLegacyTest
 *
 * @group DB
 *
 * @package Test\Files\Cache
 */
class UpdaterLegacyTest extends \Test\TestCase {
	/**
	 * @var \OC\Files\Storage\Storage $storage
	 */
	private $storage;

	/**
	 * @var \OC\Files\Cache\Scanner $scanner
	 */
	private $scanner;

	/**
	 * @var \OC\Files\Cache\Cache $cache
	 */
	private $cache;

	private static $user;

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

		$this->storage = new \OC\Files\Storage\Temporary([]);
		$textData = "dummy file data\n";
		$imgData = \file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
		$this->storage->mkdir('folder');
		$this->storage->file_put_contents('foo.txt', $textData);
		$this->storage->file_put_contents('foo.png', $imgData);
		$this->storage->file_put_contents('folder/bar.txt', $textData);
		$this->storage->file_put_contents('folder/bar2.txt', $textData);

		$this->scanner = $this->storage->getScanner();
		$this->scanner->scan('');
		$this->cache = $this->storage->getCache();

		if (!self::$user) {
			self::$user = $this->getUniqueID();
		}

		\OC::$server->getUserManager()->createUser(self::$user, 'password');
		$this->loginAsUser(self::$user);

		Filesystem::init(self::$user, '/' . self::$user . '/files');

		Filesystem::clearMounts();
		Filesystem::mount($this->storage, [], '/' . self::$user . '/files');

		\OC_Hook::clear('OC_Filesystem');
	}

	protected function tearDown(): void {
		if ($this->cache) {
			$this->cache->clear();
		}

		$result = false;
		$user = \OC::$server->getUserManager()->get(self::$user);
		if ($user !== null) {
			$result = $user->delete();
		}
		$this->assertTrue($result);

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

	public function testOverwrite() {
		$textSize = \strlen("dummy file data\n");
		$imageSize = \filesize(\OC::$SERVERROOT . '/core/img/logo.png');
		$this->cache->put('foo.txt', ['mtime' => 100, 'storage_mtime' => 150]);
		$rootCachedData1 = $this->cache->get('');
		$this->assertEquals(3 * $textSize + $imageSize, $rootCachedData1['size']);

		$oldFooCachedData = $this->cache->get('foo.txt');
		Filesystem::file_put_contents('foo.txt', 'asd');
		$newFooCachedData = $this->cache->get('foo.txt');
		$this->assertEquals(3, $newFooCachedData['size']);
		$this->assertIsString($oldFooCachedData['etag']);
		$this->assertIsString($newFooCachedData['etag']);
		$this->assertNotSame($oldFooCachedData['etag'], $newFooCachedData['etag']);
		$this->assertGreaterThanOrEqual($rootCachedData1['mtime'], $newFooCachedData['mtime']);
		$this->assertGreaterThanOrEqual($oldFooCachedData['mtime'], $newFooCachedData['mtime']);
		$rootCachedData2 = $this->cache->get('');
		$this->assertEquals(2 * $textSize + $imageSize + 3, $rootCachedData2['size']);
		$this->assertIsString($rootCachedData1['etag']);
		$this->assertIsString($rootCachedData2['etag']);
		$this->assertNotSame($rootCachedData1['etag'], $rootCachedData2['etag']);
		$this->assertGreaterThanOrEqual($rootCachedData1['mtime'], $rootCachedData2['mtime']);
	}

	public function testWriteNewFile() {
		$textSize = \strlen("dummy file data\n");
		$imageSize = \filesize(\OC::$SERVERROOT . '/core/img/logo.png');
		$rootCachedData1 = $this->cache->get('');
		$this->assertEquals(3 * $textSize + $imageSize, $rootCachedData1['size']);
		$this->assertIsString($rootCachedData1['etag']);

		$this->assertFalse($this->cache->inCache('bar.txt'));
		Filesystem::file_put_contents('bar.txt', 'asd');
		$this->assertTrue($this->cache->inCache('bar.txt'));
		$barCachedData = $this->cache->get('bar.txt');
		$this->assertEquals(3, $barCachedData['size']);
		$rootCachedData2 = $this->cache->get('');
		$this->assertEquals(3 * $textSize + $imageSize + 3, $rootCachedData2['size']);
		$this->assertIsString($rootCachedData2['etag']);
		$this->assertNotSame($rootCachedData1['etag'], $rootCachedData2['etag']);
		$this->assertGreaterThanOrEqual($rootCachedData1['mtime'], $rootCachedData2['mtime']);
		$this->assertGreaterThanOrEqual($rootCachedData1['mtime'], $barCachedData['mtime']);
		$this->assertGreaterThanOrEqual($barCachedData['mtime'], $rootCachedData2['mtime']);
	}

	public function testWriteWithMountPoints() {
		$storage2 = new \OC\Files\Storage\Temporary([]);
		$storage2->getScanner()->scan(''); //initialize etags
		$cache2 = $storage2->getCache();
		Filesystem::mount($storage2, [], '/' . self::$user . '/files/folder/substorage');
		$view = new View('/' . self::$user . '/files');
		$folderCachedData = $view->getFileInfo('folder');
		$substorageCachedData = $cache2->get('');
{'fraction_non_alphanumeric': 0.22074257984426138, 'fraction_numerical': 0.00846405597562352, 'mean_word_length': 5.307473309608541, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 165, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4929094', 'n_tokens_mistral': 3291, 'n_tokens_neox': 3099, 'n_words': 454}