]> source.dussan.org Git - nextcloud-server.git/commitdiff
add unit tests for smb notify backend
authorRobin Appelman <robin@icewind.nl>
Fri, 6 Jan 2017 15:34:29 +0000 (16:34 +0100)
committerRobin Appelman <robin@icewind.nl>
Fri, 27 Jan 2017 09:44:45 +0000 (10:44 +0100)
Signed-off-by: Robin Appelman <robin@icewind.nl>
apps/files_external/lib/Lib/Storage/SMB.php
apps/files_external/tests/Storage/SmbTest.php

index c73288f8cc20eb53fe57582ca63686b6b0c530cc..690f8e2a3341302d4120e9ecacd31c147999cbe2 100644 (file)
@@ -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);
        }
index 150d4d3a0359faed404c816fd1ae5a0db00ff8de..45c01a0c59eae9d0e272d751d4da9b5e68f109e0 100644 (file)
 
 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);
+       }
 }