adapt file hooks test to eventdispatcher utilization

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
Arthur Schiwon 2019-09-09 23:28:51 +02:00
parent 3a4e31ef6b
commit 1cc6f34d88
No known key found for this signature in database
GPG key ID: 7424F1874854DF23

View file

@ -9,12 +9,15 @@
namespace Test\Files\Node; namespace Test\Files\Node;
use OC\Files\Filesystem; use OC\Files\Filesystem;
use OC\Files\Node\HookConnector;
use OC\Files\Node\Root; use OC\Files\Node\Root;
use OC\Files\Storage\Temporary; use OC\Files\Storage\Temporary;
use OC\Files\View; use OC\Files\View;
use OCP\Files\Node; use OCP\Files\Node;
use OCP\ILogger; use OCP\ILogger;
use OCP\IUserManager; use OCP\IUserManager;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Test\TestCase; use Test\TestCase;
use Test\Traits\MountProviderTrait; use Test\Traits\MountProviderTrait;
use Test\Traits\UserTrait; use Test\Traits\UserTrait;
@ -29,6 +32,9 @@ use Test\Traits\UserTrait;
class HookConnectorTest extends TestCase { class HookConnectorTest extends TestCase {
use UserTrait; use UserTrait;
use MountProviderTrait; use MountProviderTrait;
/** @var \PHPUnit\Framework\MockObject\MockObject */
/** @var EventDispatcherInterface */
protected $eventDispatcher;
/** /**
* @var View * @var View
@ -60,6 +66,7 @@ class HookConnectorTest extends TestCase {
$this->createMock(ILogger::class), $this->createMock(ILogger::class),
$this->createMock(IUserManager::class) $this->createMock(IUserManager::class)
); );
$this->eventDispatcher = \OC::$server->getEventDispatcher();
} }
public function tearDown() { public function tearDown() {
@ -72,50 +79,50 @@ class HookConnectorTest extends TestCase {
return [ return [
[function () { [function () {
Filesystem::file_put_contents('test.txt', 'asd'); Filesystem::file_put_contents('test.txt', 'asd');
}, 'preWrite'], }, 'preWrite', '\OCP\Files::preWrite'],
[function () { [function () {
Filesystem::file_put_contents('test.txt', 'asd'); Filesystem::file_put_contents('test.txt', 'asd');
}, 'postWrite'], }, 'postWrite', '\OCP\Files::postWrite'],
[function () { [function () {
Filesystem::file_put_contents('test.txt', 'asd'); Filesystem::file_put_contents('test.txt', 'asd');
}, 'preCreate'], }, 'preCreate', '\OCP\Files::preCreate'],
[function () { [function () {
Filesystem::file_put_contents('test.txt', 'asd'); Filesystem::file_put_contents('test.txt', 'asd');
}, 'postCreate'], }, 'postCreate', '\OCP\Files::postCreate'],
[function () { [function () {
Filesystem::mkdir('test.txt'); Filesystem::mkdir('test.txt');
}, 'preCreate'], }, 'preCreate', '\OCP\Files::preCreate'],
[function () { [function () {
Filesystem::mkdir('test.txt'); Filesystem::mkdir('test.txt');
}, 'postCreate'], }, 'postCreate', '\OCP\Files::postCreate'],
[function () { [function () {
Filesystem::touch('test.txt'); Filesystem::touch('test.txt');
}, 'preTouch'], }, 'preTouch', '\OCP\Files::preTouch'],
[function () { [function () {
Filesystem::touch('test.txt'); Filesystem::touch('test.txt');
}, 'postTouch'], }, 'postTouch', '\OCP\Files::postTouch'],
[function () { [function () {
Filesystem::touch('test.txt'); Filesystem::touch('test.txt');
}, 'preCreate'], }, 'preCreate', '\OCP\Files::preCreate'],
[function () { [function () {
Filesystem::touch('test.txt'); Filesystem::touch('test.txt');
}, 'postCreate'], }, 'postCreate', '\OCP\Files::postCreate'],
[function () { [function () {
Filesystem::file_put_contents('test.txt', 'asd'); Filesystem::file_put_contents('test.txt', 'asd');
Filesystem::unlink('test.txt'); Filesystem::unlink('test.txt');
}, 'preDelete'], }, 'preDelete', '\OCP\Files::preDelete'],
[function () { [function () {
Filesystem::file_put_contents('test.txt', 'asd'); Filesystem::file_put_contents('test.txt', 'asd');
Filesystem::unlink('test.txt'); Filesystem::unlink('test.txt');
}, 'postDelete'], }, 'postDelete', '\OCP\Files::postDelete'],
[function () { [function () {
Filesystem::mkdir('test.txt'); Filesystem::mkdir('test.txt');
Filesystem::rmdir('test.txt'); Filesystem::rmdir('test.txt');
}, 'preDelete'], }, 'preDelete', '\OCP\Files::preDelete'],
[function () { [function () {
Filesystem::mkdir('test.txt'); Filesystem::mkdir('test.txt');
Filesystem::rmdir('test.txt'); Filesystem::rmdir('test.txt');
}, 'postDelete'], }, 'postDelete', '\OCP\Files::postDelete'],
]; ];
} }
@ -124,8 +131,8 @@ class HookConnectorTest extends TestCase {
* @param string $expectedHook * @param string $expectedHook
* @dataProvider viewToNodeProvider * @dataProvider viewToNodeProvider
*/ */
public function testViewToNode(callable $operation, $expectedHook) { public function testViewToNode(callable $operation, $expectedHook, $expectedEvent) {
$connector = new \OC\Files\Node\HookConnector($this->root, $this->view); $connector = new HookConnector($this->root, $this->view, $this->eventDispatcher);
$connector->viewToNode(); $connector->viewToNode();
$hookCalled = false; $hookCalled = false;
/** @var Node $hookNode */ /** @var Node $hookNode */
@ -136,10 +143,21 @@ class HookConnectorTest extends TestCase {
$hookNode = $node; $hookNode = $node;
}); });
$dispatcherCalled = false;
/** @var Node $dispatcherNode */
$dispatcherNode = null;
$this->eventDispatcher->addListener($expectedEvent, function (GenericEvent $event) use (&$dispatcherCalled, &$dispatcherNode) {
$dispatcherCalled = true;
$dispatcherNode = $event->getSubject();
});
$operation(); $operation();
$this->assertTrue($hookCalled); $this->assertTrue($hookCalled);
$this->assertEquals('/' . $this->userId . '/files/test.txt', $hookNode->getPath()); $this->assertEquals('/' . $this->userId . '/files/test.txt', $hookNode->getPath());
$this->assertTrue($dispatcherCalled);
$this->assertEquals('/' . $this->userId . '/files/test.txt', $dispatcherNode->getPath());
} }
public function viewToNodeProviderCopyRename() { public function viewToNodeProviderCopyRename() {
@ -147,19 +165,19 @@ class HookConnectorTest extends TestCase {
[function () { [function () {
Filesystem::file_put_contents('source', 'asd'); Filesystem::file_put_contents('source', 'asd');
Filesystem::rename('source', 'target'); Filesystem::rename('source', 'target');
}, 'preRename'], }, 'preRename', '\OCP\Files::preRename'],
[function () { [function () {
Filesystem::file_put_contents('source', 'asd'); Filesystem::file_put_contents('source', 'asd');
Filesystem::rename('source', 'target'); Filesystem::rename('source', 'target');
}, 'postRename'], }, 'postRename', '\OCP\Files::postRename'],
[function () { [function () {
Filesystem::file_put_contents('source', 'asd'); Filesystem::file_put_contents('source', 'asd');
Filesystem::copy('source', 'target'); Filesystem::copy('source', 'target');
}, 'preCopy'], }, 'preCopy', '\OCP\Files::preCopy'],
[function () { [function () {
Filesystem::file_put_contents('source', 'asd'); Filesystem::file_put_contents('source', 'asd');
Filesystem::copy('source', 'target'); Filesystem::copy('source', 'target');
}, 'postCopy'], }, 'postCopy', '\OCP\Files::postCopy'],
]; ];
} }
@ -168,8 +186,8 @@ class HookConnectorTest extends TestCase {
* @param string $expectedHook * @param string $expectedHook
* @dataProvider viewToNodeProviderCopyRename * @dataProvider viewToNodeProviderCopyRename
*/ */
public function testViewToNodeCopyRename(callable $operation, $expectedHook) { public function testViewToNodeCopyRename(callable $operation, $expectedHook, $expectedEvent) {
$connector = new \OC\Files\Node\HookConnector($this->root, $this->view); $connector = new HookConnector($this->root, $this->view, $this->eventDispatcher);
$connector->viewToNode(); $connector->viewToNode();
$hookCalled = false; $hookCalled = false;
/** @var Node $hookSourceNode */ /** @var Node $hookSourceNode */
@ -183,15 +201,29 @@ class HookConnectorTest extends TestCase {
$hookTargetNode = $targetNode; $hookTargetNode = $targetNode;
}); });
$dispatcherCalled = false;
/** @var Node $dispatcherSourceNode */
$dispatcherSourceNode = null;
/** @var Node $dispatcherTargetNode */
$dispatcherTargetNode = null;
$this->eventDispatcher->addListener($expectedEvent, function (GenericEvent $event) use (&$dispatcherSourceNode, &$dispatcherTargetNode, &$dispatcherCalled) {
$dispatcherCalled = true;
list($dispatcherSourceNode, $dispatcherTargetNode) = $event->getSubject();
});
$operation(); $operation();
$this->assertTrue($hookCalled); $this->assertTrue($hookCalled);
$this->assertEquals('/' . $this->userId . '/files/source', $hookSourceNode->getPath()); $this->assertEquals('/' . $this->userId . '/files/source', $hookSourceNode->getPath());
$this->assertEquals('/' . $this->userId . '/files/target', $hookTargetNode->getPath()); $this->assertEquals('/' . $this->userId . '/files/target', $hookTargetNode->getPath());
$this->assertTrue($dispatcherCalled);
$this->assertEquals('/' . $this->userId . '/files/source', $dispatcherSourceNode->getPath());
$this->assertEquals('/' . $this->userId . '/files/target', $dispatcherTargetNode->getPath());
} }
public function testPostDeleteMeta() { public function testPostDeleteMeta() {
$connector = new \OC\Files\Node\HookConnector($this->root, $this->view); $connector = new HookConnector($this->root, $this->view, $this->eventDispatcher);
$connector->viewToNode(); $connector->viewToNode();
$hookCalled = false; $hookCalled = false;
/** @var Node $hookNode */ /** @var Node $hookNode */
@ -202,11 +234,22 @@ class HookConnectorTest extends TestCase {
$hookNode = $node; $hookNode = $node;
}); });
$dispatcherCalled = false;
/** @var Node $dispatcherNode */
$dispatcherNode = null;
$this->eventDispatcher->addListener('\OCP\Files::postDelete', function (GenericEvent $event) use (&$dispatcherCalled, &$dispatcherNode) {
$dispatcherCalled = true;
$dispatcherNode = $event->getSubject();
});
Filesystem::file_put_contents('test.txt', 'asd'); Filesystem::file_put_contents('test.txt', 'asd');
$info = Filesystem::getFileInfo('test.txt'); $info = Filesystem::getFileInfo('test.txt');
Filesystem::unlink('test.txt'); Filesystem::unlink('test.txt');
$this->assertTrue($hookCalled); $this->assertTrue($hookCalled);
$this->assertEquals($hookNode->getId(), $info->getId()); $this->assertEquals($hookNode->getId(), $info->getId());
$this->assertTrue($dispatcherCalled);
$this->assertEquals($dispatcherNode->getId(), $info->getId());
} }
} }