From 1432502e62af2527c879d94496eb9f0e0891d25a Mon Sep 17 00:00:00 2001 From: nathangray Date: Wed, 16 Sep 2020 15:05:43 -0600 Subject: [PATCH] Some more test work - Refactor some simple tests into base for reuse - Add (simple) tests for links:// --- api/src/Vfs/Base.php | 2 +- api/tests/Vfs/Links/StreamWrapperTest.php | 81 +++++++++++++++++ api/tests/Vfs/StreamWrapperBase.php | 101 ++++++++++++++++++---- api/tests/Vfs/StreamWrapperTest.php | 59 ++----------- 4 files changed, 172 insertions(+), 71 deletions(-) create mode 100644 api/tests/Vfs/Links/StreamWrapperTest.php diff --git a/api/src/Vfs/Base.php b/api/src/Vfs/Base.php index 6b7c727314..ab0b745f8e 100644 --- a/api/src/Vfs/Base.php +++ b/api/src/Vfs/Base.php @@ -164,7 +164,7 @@ class Base } unset(self::$fstab[$path]); - Api\Config::save_value('vfs_fstab',self::$fstab,'phpgwapi'); + \EGroupware\Api\Config::save_value('vfs_fstab',self::$fstab,'phpgwapi'); $GLOBALS['egw_info']['server']['vfs_fstab'] = self::$fstab; // invalidate session cache if (method_exists($GLOBALS['egw'],'invalidate_session_cache')) // egw object in setup is limited diff --git a/api/tests/Vfs/Links/StreamWrapperTest.php b/api/tests/Vfs/Links/StreamWrapperTest.php new file mode 100644 index 0000000000..190ad4e2c2 --- /dev/null +++ b/api/tests/Vfs/Links/StreamWrapperTest.php @@ -0,0 +1,81 @@ +mountLinks('/apps'); + + $info_id = $this->make_infolog(); + $this->files[] = $this->test_file = $this->getFilename(null, $info_id); + + // Check that the file is not there + $pre_start = Vfs::stat($this->test_file); + $this->assertEquals(null,$pre_start, + "File '$this->test_file' was there before we started, check clean up" + ); + + } + + protected function tearDown() : void + { + // Do local stuff first, parent will remove stuff that is needed + + $bo = new \infolog_bo(); + foreach($this->entries as $entry) + { + $bo->delete($entry); + } + + parent::tearDown(); + } + + /** + * Make an infolog entry + */ + protected function make_infolog() + { + $bo = new \infolog_bo(); + $element = array( + 'info_subject' => "Test infolog for #{$this->getName()}", + 'info_des' => 'Test element for ' . $this->getName() . "\n" . Api\DateTime::to(), + 'info_status' => 'open' + ); + + $element_id = $bo->write($element, true, true, true, true); + $this->entries[] = $element_id; + return $element_id; + } + /** + * Make a filename that reflects the current test + */ + protected function getFilename($path, $info_id) + { + if(is_null($path)) $path = '/apps/infolog/'; + if(substr($path,-1,1) !== '/') $path = $path . '/'; + $reflect = new \ReflectionClass($this); + return $path .$info_id .'/'. $reflect->getShortName() . '_' . $this->getName() . '.txt'; + } + +} \ No newline at end of file diff --git a/api/tests/Vfs/StreamWrapperBase.php b/api/tests/Vfs/StreamWrapperBase.php index 2100a39958..bb194df270 100644 --- a/api/tests/Vfs/StreamWrapperBase.php +++ b/api/tests/Vfs/StreamWrapperBase.php @@ -32,6 +32,11 @@ class StreamWrapperBase extends LoggedInTest */ const LOG_LEVEL = 0; + /** + * @var string If we're just doing a simple test with one file, use this file + */ + protected $test_file = ''; + /** * Keep track of files to remove after * @var Array @@ -115,7 +120,83 @@ class StreamWrapperBase extends LoggedInTest if(is_null($path)) $path = Vfs::get_home_dir().'/'; if(substr($path,-1,1) !== '/') $path = $path . '/'; - return $path . get_class(this) . '_' . $this->getName() . '.txt'; + $reflect = new \ReflectionClass($this); + return $path . $reflect->getShortName() . '_' . $this->getName() . '.txt'; + } + + /** + * Simple test that we can write something and it's there + * By putting it in the base class, this test gets run for every backend + */ + public function testSimpleReadWrite() : void + { + if(!$this->test_file) + { + $this->markTestSkipped("No test file set - set it in setUp() or overriding test"); + } + $contents = $this->getName() . "\nJust a test ;)\n"; + $this->assertNotFalse( + file_put_contents(Vfs::PREFIX . $this->test_file, $contents), + "Could not write file $this->test_file" + ); + + // Check contents are unchanged + $this->assertEquals( + $contents, file_get_contents(Vfs::PREFIX . $this->test_file), + "Read file contents do not match what was written" + ); + } + /** + * Simple delete of a file + * By putting it in the base class, this test gets run for every backend + * + */ + public function testDelete() : void + { + if(!$this->test_file) + { + $this->markTestSkipped("No test file set - set it in setUp() or overriding test"); + } + + // Write + $contents = $this->getName() . "\nJust a test ;)\n"; + $this->assertNotFalse( + file_put_contents(Vfs::PREFIX . $this->test_file, $contents), + "Could not write file $this->test_file" + ); + + $start = Vfs::stat($this->test_file); + $this->assertNotNull( + $start, + "File '$this->test_file' was not what we expected to find after writing" + ); + + Vfs::unlink($this->test_file); + + $post = Vfs::stat($this->test_file); + $this->assertEquals(null,$post, + "File '$this->test_file' was there after deleting" + ); + } + + /** + * 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), + "Unabe to mount $url => $path" + ); + Vfs::$is_root = false; + + $this->mounts[] = $path; + Vfs::clearstatcache(); + Vfs::init_static(); } /** @@ -187,7 +268,7 @@ class StreamWrapperBase extends LoggedInTest Vfs::$is_root = true; // I guess merge needs the dir in SQLFS first - if(!Vfs::is_dir($dir)) Vfs::mkdir($path); + if(!Vfs::is_dir($path)) Vfs::mkdir($path); Vfs::chmod($path, 0750); Vfs::chown($path, $GLOBALS['egw_info']['user']['account_id']); @@ -265,20 +346,4 @@ class StreamWrapperBase extends LoggedInTest */ return $files; } - - /** - * Make an infolog entry - */ - protected function make_infolog() - { - $bo = new \infolog_bo(); - $element = array( - 'info_subject' => "Test infolog for #{$this->getName()}", - 'info_des' => 'Test element for ' . $this->getName() . "\n" . Api\DateTime::to(), - 'info_status' => 'open' - ); - - $element_id = $bo->write($element, true, true, true, true); - return $element_id; - } } \ No newline at end of file diff --git a/api/tests/Vfs/StreamWrapperTest.php b/api/tests/Vfs/StreamWrapperTest.php index 1feb73bd1e..73a580a8dc 100644 --- a/api/tests/Vfs/StreamWrapperTest.php +++ b/api/tests/Vfs/StreamWrapperTest.php @@ -25,6 +25,13 @@ class StreamWrapperTest extends StreamWrapperBase { parent::setUp(); + $this->files[] = $this->test_file = $this->getFilename(); + + // Check that the file is not there + $pre_start = Vfs::stat($this->test_file); + $this->assertEquals(null,$pre_start, + "File '$this->test_file' was there before we started, check clean up" + ); } protected function tearDown() : void @@ -34,56 +41,4 @@ class StreamWrapperTest extends StreamWrapperBase parent::tearDown(); } - /** - * Simple test that we can write something and it's there - */ - public function testSimpleReadWrite() : void - { - $this->files[] = $test_file = $this->getFilename(); - $contents = $this->getName() . "\nJust a test ;)\n"; - $this->assertNotFalse( - file_put_contents(Vfs::PREFIX . $test_file, $contents), - "Could not write file $test_file" - ); - - // Check contents are unchanged - $this->assertEquals( - $contents, file_get_contents(Vfs::PREFIX . $test_file), - "Read file contents do not match what was written" - ); - } - - /** - * Simple delete of a file - */ - public function testDelete() : void - { - $this->files[] = $test_file = $this->getFilename(); - - // Check that the file is not there - $pre_start = Vfs::stat($test_file); - $this->assertEquals(null,$pre_start, - "File '$test_file' was there before we started, check clean up" - ); - - // Write - $contents = $this->getName() . "\nJust a test ;)\n"; - $this->assertNotFalse( - file_put_contents(Vfs::PREFIX . $test_file, $contents), - "Could not write file $test_file" - ); - - $start = Vfs::stat($test_file); - $this->assertNotNull( - $start, - "File '$test_file' was not what we expected to find after writing" - ); - - Vfs::unlink($test_file); - - $post = Vfs::stat($test_file); - $this->assertEquals(null,$post, - "File '$test_file' was there after deleting" - ); - } } \ No newline at end of file