<?php
/**
* SeekQuarry/Yioop --
* Open Source Pure PHP Search Engine, Crawler, and Indexer
*
* Copyright (C) 2009 - 2026 Chris Pollett chris@pollett.org
*
* LICENSE:
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* END LICENSE
*
* @author Chris Pollett chris@pollett.org
* @license https://www.gnu.org/licenses/ GPL3
* @link https://www.seekquarry.com/
* @copyright 2009 - 2026
* @filesource
*/
namespace seekquarry\yioop\tests;
use seekquarry\yioop\configs as C;
use seekquarry\yioop\library\version_control\VersionManager;
use seekquarry\yioop\library\UnitTest;
/**
* UnitTests for the VersionManager class.
*
* @author Chris Pollett
*/
class VersionManagerTest extends UnitTest
{
/**
* our dbms manager handle so we can call unlinkRecursive
* @var object
*/
public $db;
/**
* Folder to use for test repository
* @var string
*/
public $version_test_folder = C\TEST_DIR .
"/test_files/version_manager_test";
/**
* Sets up a minimal DBMS manager class so that we will be able to use
* unlinkRecursive to tear down the files created bby our tests
*/
public function __construct()
{
$db_class = C\NS_DATASOURCES . ucfirst(C\DBMS)."Manager";
$this->db = new $db_class();
if (!file_exists($this->version_test_folder)) {
mkdir($this->version_test_folder);
}
}
/**
* Does nothing
*/
public function setUp()
{
}
/**
* Delete the files created associated with the VersionManager tests
*/
public function tearDown()
{
$this->db->unlinkRecursive($this->version_test_folder, false);
}
/**
* Test the ability to create a new version of a folder within the
* VervionManager archive.
*/
public function createVersionFolderTestCase()
{
file_put_contents($this->version_test_folder . "/test.txt", "hi there");
$this->assertTrue(file_exists($this->version_test_folder . "/test.txt"),
"Write anything test folder");
$vcs = new VersionManager($this->version_test_folder);
$this->assertTrue(file_exists($this->version_test_folder . "/.archive"),
"Archive sub-folder created");
$archive_info = $vcs->headInfo();
$this->assertTrue(!empty($archive_info),
"HEAD info file exists and not empty");
$this->assertTrue(!empty($archive_info['TIMESTAMP']),
"HEAD version timestamp exists");
$this->assertTrue(!empty($archive_info['FILES']['test.txt']),
"First test file is in stored in HEAD info");
}
/**
* Tests that when a manager is constructed with versioning off, as is
* done for a git repository page's resource folder, it creates no
* archive folder and takes no snapshots, while a write still succeeds.
*/
public function versioningOffSkipsArchiveTestCase()
{
$repo = $this->version_test_folder . "/repo";
mkdir($repo, 0777, true);
$vcs = new VersionManager($repo, versioning: false);
$this->assertFalse($vcs->versioning,
"versioning is off when constructed with versioning false");
$this->assertFalse(file_exists($repo . "/.archive"),
"no archive folder is created when versioning is off");
$put = $vcs->headPutContents($repo . "/note.txt", "data");
$this->assertEqual(VersionManager::SUCCESS, $put,
"a write still succeeds without versioning");
$this->assertFalse(file_exists($repo . "/.archive"),
"still no archive folder after a write");
}
/**
* Tests restoring a folder to a given timestamp, making sure the
* correct files are present after the restore.
*/
public function restoreVersionTestCase()
{
$timestamps = [];
$vcs = new VersionManager($this->version_test_folder);
$head_info = $vcs->headInfo();
$timestamps["initial"] = $head_info['TIMESTAMP'];
foreach (["test1.txt", "test2.txt"] as $test_file) {
$file_name = $this->version_test_folder . "/$test_file";
$vcs->headPutContents($file_name, $test_file);
$head_info = $vcs->headInfo();
$timestamps[$test_file] = $head_info['TIMESTAMP'];
}
$dir = $this->version_test_folder . "/test";
$vcs->headMakeDirectory($dir);
$this->assertTrue(file_exists($dir),
"headMakeDirectory succeeded");
symlink(C\TEST_DIR . "/test_files/test.pdf", $dir . "/test.pdf");
$vcs->createVersion($dir . "/test.pdf");
$head_info = $vcs->headInfo();
$timestamps["symlink"] = $head_info['TIMESTAMP'];
$vcs->restoreVersion($timestamps["initial"]);
clearstatcache();
$this->assertTrue(!file_exists($this->version_test_folder .
"/test1.txt"),
"After Restore later file not present 1");
$vcs->restoreVersion($timestamps["test1.txt"]);
clearstatcache();
$this->assertTrue(file_exists($this->version_test_folder .
"/test1.txt"),
"After restore file that should be present is");
$this->assertTrue(!file_exists($this->version_test_folder .
"/test2.txt"),
"After Restore later file not present 2");
clearstatcache();
$vcs->restoreVersion($timestamps["symlink"]);
$this->assertTrue(file_exists($dir . "/test.pdf"),
"After Restore symlink restored");
}
}