From 97283bad76e8157d86f62330deff42a2d3b47f07 Mon Sep 17 00:00:00 2001 From: nathangray Date: Mon, 21 Sep 2020 16:38:38 -0600 Subject: [PATCH] Some more test work - Fix some existing tests so they don't fail due to changes - Add tests for Vfs\Sharing\StreamWrapper --- api/tests/LoggedInTest.php | 12 +- .../Vfs/Filesystem/StreamWrapperTest.php | 2 +- api/tests/Vfs/Links/StreamWrapperTest.php | 2 +- api/tests/Vfs/Sharing/StreamWrapperTest.php | 117 ++++++++++++++++++ api/tests/Vfs/SharingACLTest.php | 8 +- api/tests/Vfs/SharingBackendTest.php | 10 +- api/tests/Vfs/SharingBase.php | 28 ++++- api/tests/Vfs/StreamWrapperBase.php | 2 +- api/tests/Vfs/StreamWrapperTest.php | 2 +- 9 files changed, 162 insertions(+), 21 deletions(-) create mode 100644 api/tests/Vfs/Sharing/StreamWrapperTest.php diff --git a/api/tests/LoggedInTest.php b/api/tests/LoggedInTest.php index 0a357bab56..b4ac0df9fc 100644 --- a/api/tests/LoggedInTest.php +++ b/api/tests/LoggedInTest.php @@ -74,13 +74,13 @@ abstract class LoggedInTest extends TestCase */ public static function tearDownAfterClass() : void { - // Clean up VFS - Vfs::clearstatcache(); - // Reset stream context, or current user will always be there - stream_context_set_option(stream_context_get_default(),['vfs'=>['user' => null]]); + // Clean up VFS + Vfs::clearstatcache(); + // Reset stream context, or current user will always be there + stream_context_set_option(stream_context_get_default(),['vfs'=>['user' => null]]); - // Clear some link caching - Link::init_static(true); + // Clear some link caching + Link::init_static(true); if($GLOBALS['egw']) { diff --git a/api/tests/Vfs/Filesystem/StreamWrapperTest.php b/api/tests/Vfs/Filesystem/StreamWrapperTest.php index bd65b1b127..b35dcb11be 100644 --- a/api/tests/Vfs/Filesystem/StreamWrapperTest.php +++ b/api/tests/Vfs/Filesystem/StreamWrapperTest.php @@ -38,7 +38,7 @@ class StreamWrapperTest extends Vfs\StreamWrapperBase $this->mountFilesystem(static::$mountpoint); } - protected function allowAccess(string $test_name, string $test_file, int $test_user, string $needed) : void + protected function allowAccess(string $test_name, string &$test_file, int $test_user, string $needed) : void { // We'll allow access by putting test user in Default group $command = new \admin_cmd_edit_user($test_user, ['account_groups' => array_merge($this->account['account_groups'],['Default'])]); diff --git a/api/tests/Vfs/Links/StreamWrapperTest.php b/api/tests/Vfs/Links/StreamWrapperTest.php index de4275127d..502d709493 100644 --- a/api/tests/Vfs/Links/StreamWrapperTest.php +++ b/api/tests/Vfs/Links/StreamWrapperTest.php @@ -64,7 +64,7 @@ class StreamWrapperTest extends Vfs\StreamWrapperBase parent::testWithAccess(); } - protected function allowAccess(string $test_name, string $test_file, int $test_user, string $needed) : void + protected function allowAccess(string $test_name, string &$test_file, int $test_user, string $needed) : void { // We'll allow access by putting test user in responsible $so = new \infolog_so(); diff --git a/api/tests/Vfs/Sharing/StreamWrapperTest.php b/api/tests/Vfs/Sharing/StreamWrapperTest.php new file mode 100644 index 0000000000..d15388837e --- /dev/null +++ b/api/tests/Vfs/Sharing/StreamWrapperTest.php @@ -0,0 +1,117 @@ +createShare(); + parent::setUp(); + } + + protected function tearDown() : void + { + parent::tearDown(); + } + + public function testSimpleReadWrite(): string + { + $this->files[] = $this->test_file = $this->getFilename('',false); + + return parent::testSimpleReadWrite(); + } + + public function testNoReadAccess(): void + { + $this->files[] = $this->test_file = $this->getFilename('',false); + + parent::testNoReadAccess(); + } + + public function testWithAccess(): void + { + $this->files[] = $this->test_file = $this->getFilename('',false); + + parent::testWithAccess(); + } + + protected function allowAccess(string $test_name, string &$test_file, int $test_user, string $needed) : void + { + // Anyone who mounts will have access, but the available path changes + $test_file = '/home/'. $GLOBALS['egw']->accounts->id2name($test_user) . '/' . + Vfs\Sharing::SHARES_DIRECTORY .'/'.static::$test_dir .'/'. Vfs::basename($test_file); + } + + public function mount() : void + { + Api\Vfs\Sharing::setup_share(true,$this->share); + } + + public function createShare(&$dir='', $extra = array(), $create = 'createShare') + { + // First, create the directory to be shared + $this->files[] = $dir = Vfs::get_home_dir() . '/'. static::$test_dir; + Vfs::mkdir($dir); + + // Create and use link + $this->getShareExtra($dir, Sharing::WRITABLE, $extra); + + $this->share = Vfs\Sharing::create('',$dir,Sharing::WRITABLE,$dir,'',$extra); + $link = Vfs\Sharing::share2link($this->share); + + return $link; + } + + + /** + * Get the extra information required to create a share link for the given + * directory, with the given mode + * + * @param string $dir Share target + * @param int $mode Share mode + * @param Array $extra + */ + protected function getShareExtra($dir, $mode, &$extra) + { + switch($mode) + { + case Sharing::WRITABLE: + $extra['share_writable'] = TRUE; + break; + } + } + + /** + * Make a filename that reflects the current test + * @param $path + * @param bool $mounted Get the path if the share is mounted, or the original + * @return string + */ + protected function getFilename($path = null, $mounted = true) : string + { + return parent::getFilename(Vfs::get_home_dir() . '/'. + ($mounted ? Vfs\Sharing::SHARES_DIRECTORY .'/' : '').static::$test_dir .'/'. $path); + } + +} \ No newline at end of file diff --git a/api/tests/Vfs/SharingACLTest.php b/api/tests/Vfs/SharingACLTest.php index d5c01ebf52..e957328232 100644 --- a/api/tests/Vfs/SharingACLTest.php +++ b/api/tests/Vfs/SharingACLTest.php @@ -55,6 +55,7 @@ class SharingACLTest extends SharingBase protected function tearDown() : void { + LoggedInTest::setUpBeforeClass(); parent::tearDown(); if($this->account_id) { @@ -198,9 +199,6 @@ class SharingACLTest extends SharingBase $this->assertNotNull($form, "Could not read the share link"); $rows = $data['data']['content']['nm']['rows']; - Vfs::clearstatcache(); - Vfs::init_static(); - Vfs\StreamWrapper::init_static(); // Check we can't find the non-shared file $result = array_filter($rows, function($v) { @@ -236,10 +234,6 @@ class SharingACLTest extends SharingBase $this->assertNotNull($form, "Could not read the share link"); $rows = array_values($data['data']['content']['nm']['rows']); - Vfs::clearstatcache(); - Vfs::init_static(); - Vfs\StreamWrapper::init_static(); - // Check we can't find the non-shared file $result = array_filter($rows, function($v) { return $v['name'] == $this->no_access; diff --git a/api/tests/Vfs/SharingBackendTest.php b/api/tests/Vfs/SharingBackendTest.php index 60b598b82b..5da33932a8 100644 --- a/api/tests/Vfs/SharingBackendTest.php +++ b/api/tests/Vfs/SharingBackendTest.php @@ -31,7 +31,7 @@ class SharingBackendTest extends SharingBase */ public function testHomeReadonly() { - $dir = Vfs::get_home_dir().'/'; + $dir = Vfs::get_home_dir().'/'.$this->getName(false).'/'; $this->checkDirectory($dir, Sharing::READONLY); } @@ -42,7 +42,7 @@ class SharingBackendTest extends SharingBase */ public function testHomeWritable() { - $dir = Vfs::get_home_dir().'/'; + $dir = Vfs::get_home_dir().'/'.$this->getName(false).'/'; $this->checkDirectory($dir, Sharing::WRITABLE); } @@ -124,6 +124,9 @@ class SharingBackendTest extends SharingBase */ public function testLinksReadonly() { + // Need to mount apps + $this->mountLinks("/apps"); + // Create an infolog entry for testing purposes $info_id = $this->make_infolog(); $bo = new \infolog_bo(); @@ -142,6 +145,9 @@ class SharingBackendTest extends SharingBase */ public function testLinksWritable() { + // Need to mount apps + $this->mountLinks("/apps"); + // Create an infolog entry for testing purposes $bo = new \infolog_bo(); $info_id = $this->make_infolog(); diff --git a/api/tests/Vfs/SharingBase.php b/api/tests/Vfs/SharingBase.php index fec77ca2f9..a93a714a79 100644 --- a/api/tests/Vfs/SharingBase.php +++ b/api/tests/Vfs/SharingBase.php @@ -153,6 +153,10 @@ class SharingBase extends LoggedInTest { $dir .= '/'; } + if(!Vfs::is_readable($dir)) + { + Vfs::mkdir($dir); + } $this->files += $this->addFiles($dir); $logged_in_files = array_map( @@ -230,7 +234,7 @@ class SharingBase extends LoggedInTest switch($mode) { case Sharing::READONLY: - $this->assertFalse(Vfs::is_writable($file)); + $this->assertFalse(Vfs::is_writable($file), "Readonly share file '$file' is writable"); if(!Vfs::is_dir($file)) { // We expect this to fail @@ -251,6 +255,26 @@ class SharingBase extends LoggedInTest } + /** + * Mount the app entries into the filesystem + * + * @param string $path + */ + protected function mountLinks($path) + { + Vfs::$is_root = true; + $url = Links\StreamWrapper::PREFIX . '/apps'; + $this->assertTrue( + Vfs::mount($url, $path, false, false), + "Unable to mount $url => $path" + ); + Vfs::$is_root = false; + + $this->mounts[] = $path; + Vfs::clearstatcache(); + Vfs::init_static(); + } + /** * Start versioning for the given path * @@ -565,7 +589,7 @@ class SharingBase extends LoggedInTest // Make sure we start at root, not somewhere else like the token mounted // as a sub-directory - $this->assertEquals('/', $data->data->content->nm->path); + $this->assertEquals('/', $data->data->content->nm->path, "Share was not mounted at /"); unset($data->data->content->nm->actions); //var_dump($data->data->content->nm); diff --git a/api/tests/Vfs/StreamWrapperBase.php b/api/tests/Vfs/StreamWrapperBase.php index b8aaf4f033..0b281d49a8 100644 --- a/api/tests/Vfs/StreamWrapperBase.php +++ b/api/tests/Vfs/StreamWrapperBase.php @@ -409,7 +409,7 @@ abstract class StreamWrapperBase extends LoggedInTest * @param string $needed r, w, rw * @return mixed */ - abstract protected function allowAccess(string $test_name, string $test_file, int $test_user, string $needed) : void; + abstract protected function allowAccess(string $test_name, string &$test_file, int $test_user, string $needed) : void; /** * Mount the app entries into the filesystem diff --git a/api/tests/Vfs/StreamWrapperTest.php b/api/tests/Vfs/StreamWrapperTest.php index aa88e57b13..60944b66d7 100644 --- a/api/tests/Vfs/StreamWrapperTest.php +++ b/api/tests/Vfs/StreamWrapperTest.php @@ -47,7 +47,7 @@ class StreamWrapperTest extends StreamWrapperBase // Nothing here } - protected function allowAccess(string $test_name, string $test_file, int $test_user, string $needed) : void + protected function allowAccess(string $test_name, string &$test_file, int $test_user, string $needed) : void { // We'll allow access by putting test user in Default group $command = new \admin_cmd_edit_user($test_user, ['account_groups' => array_merge($this->account['account_groups'],['Default'])]);