From d14ff700503ebef6cda27aae1dd348f75b093d09 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 6 Jan 2017 16:34:29 +0100 Subject: add unit tests for smb notify backend Signed-off-by: Robin Appelman --- apps/files_external/lib/Lib/Storage/SMB.php | 1 + apps/files_external/tests/Storage/SmbTest.php | 47 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) (limited to 'apps') diff --git a/apps/files_external/lib/Lib/Storage/SMB.php b/apps/files_external/lib/Lib/Storage/SMB.php index c73288f8cc2..690f8e2a334 100644 --- a/apps/files_external/lib/Lib/Storage/SMB.php +++ b/apps/files_external/lib/Lib/Storage/SMB.php @@ -499,6 +499,7 @@ class SMB extends Common implements INotifyStorage { } public function notify($path) { + $path = '/' . ltrim($path, '/'); $shareNotifyHandler = $this->share->notify($this->buildPath($path)); return new SMBNotifyHandler($shareNotifyHandler, $this->root); } diff --git a/apps/files_external/tests/Storage/SmbTest.php b/apps/files_external/tests/Storage/SmbTest.php index 150d4d3a035..45c01a0c59e 100644 --- a/apps/files_external/tests/Storage/SmbTest.php +++ b/apps/files_external/tests/Storage/SmbTest.php @@ -27,7 +27,10 @@ namespace OCA\Files_External\Tests\Storage; +use OC\Files\Notify\Change; +use OC\Files\Notify\RenameChange; use \OCA\Files_External\Lib\Storage\SMB; +use OCP\Files\Notify\IChange; /** * Class SmbTest @@ -37,6 +40,10 @@ use \OCA\Files_External\Lib\Storage\SMB; * @package OCA\Files_External\Tests\Storage */ class SmbTest extends \Test\Files\Storage\Storage { + /** + * @var SMB instance + */ + protected $instance; protected function setUp() { parent::setUp(); @@ -85,4 +92,44 @@ class SmbTest extends \Test\Files\Storage\Storage { $this->assertEquals('smb::testuser@testhost//someshare//someroot/', $this->instance->getId()); $this->instance = null; } + + public function testNotifyGetChanges() { + $notifyHandler = $this->instance->notify(''); + usleep(100 * 1000); //give time for the notify to start + $this->instance->file_put_contents('/newfile.txt', 'test content'); + $this->instance->rename('/newfile.txt', 'renamed.txt'); + $this->instance->unlink('/renamed.txt'); + usleep(100 * 1000); //time for all changes to be processed + + $changes = $notifyHandler->getChanges(); + $notifyHandler->stop(); + + $expected = [ + new Change(IChange::ADDED, 'newfile.txt'), + new RenameChange(IChange::RENAMED, 'newfile.txt', 'renamed.txt'), + new Change(IChange::REMOVED, 'renamed.txt') + ]; + + foreach ($expected as $expectedChange) { + $this->assertContains($expectedChange, $changes, '', false, false); // dont check object identity + } + } + + public function testNotifyListen() { + $notifyHandler = $this->instance->notify(''); + usleep(100 * 1000); //give time for the notify to start + $this->instance->file_put_contents('/newfile.txt', 'test content'); + $this->instance->unlink('/newfile.txt'); + usleep(100 * 1000); //time for all changes to be processed + + $result = null; + + // since the notify handler buffers untill we start listening we will get the above changes + $notifyHandler->listen(function (IChange $change) use (&$result) { + $result = $change; + return false;//stop listening + }); + + $this->assertEquals(new Change(IChange::ADDED, 'newfile.txt'), $result); + } } -- cgit v1.2.3