Commit 48b4ec6b authored by Victor Dubiniuk's avatar Victor Dubiniuk
Browse files

Do not use listener in test

parent d8c2c63f
......@@ -39,6 +39,7 @@ use OCP\IUser;
use OCP\IConfig;
use OCP\UserInterface;
use \OCP\IUserBackend;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\GenericEvent;
class User implements IUser {
......@@ -72,17 +73,24 @@ class User implements IUser {
/** @var IURLGenerator */
private $urlGenerator;
/** @var EventDispatcher */
private $eventDispatcher;
/**
* @param string $uid
* @param UserInterface $backend
* @param \OC\Hooks\Emitter $emitter
* @param IConfig|null $config
* @param IURLGenerator $urlGenerator
* @param EventDispatcher $eventDispatcher
*/
public function __construct($uid, $backend, $emitter = null, IConfig $config = null, $urlGenerator = null) {
public function __construct($uid, $backend, $emitter = null, IConfig $config = null,
$urlGenerator = null, EventDispatcher $eventDispatcher = null
) {
$this->uid = $uid;
$this->backend = $backend;
$this->emitter = $emitter;
$this->eventDispatcher = $eventDispatcher;
if(is_null($config)) {
$config = \OC::$server->getConfig();
}
......@@ -339,8 +347,9 @@ class User implements IUser {
$enabled = ($enabled) ? 'true' : 'false';
$this->config->setUserValue($this->uid, 'core', 'enabled', $enabled);
$eventDispatcher = \OC::$server->getEventDispatcher();
$eventDispatcher->dispatch(self::class . '::postSetEnabled', new GenericEvent($this));
if ($this->eventDispatcher){
$this->eventDispatcher->dispatch(self::class . '::postSetEnabled', new GenericEvent($this));
}
}
/**
......
......@@ -14,6 +14,7 @@ use OC\User\Backend;
use OC\User\Database;
use OC\User\User;
use OCP\IConfig;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\GenericEvent;
use Test\TestCase;
use Test\Util\User\Dummy;
......@@ -504,29 +505,31 @@ class UserTest extends TestCase {
}
public function testSetEnabledHook(){
$hooksCalled = 0;
$test = $this;
/**
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
*/
$backend = $this->createMock(Dummy::class);
/**
* @param GenericEvent $event
*/
$hook = function ($event) use ($test, &$hooksCalled) {
$hooksCalled++;
$test->assertEquals('foo', $event->getSubject()->getUID());
};
$eventDispatcher = \OC::$server->getEventDispatcher();
$eventDispatcher->addListener(User::class . '::postSetEnabled', $hook);
$user = new User('foo', $backend, new PublicEmitter());
$user->setEnabled(true);
$eventDispatcherMock = $this->createMock(EventDispatcher::class);
$expectations = [true, false];
$eventDispatcherMock->expects($this->exactly(2))
->method('dispatch')
->with(
$this->callback(
function($eventName){
if ($eventName === User::class . '::postSetEnabled' ){
return true;
}
return false;
}
),
$this->anything()
)
;
$user = new User('foo', $backend, null, null, null, $eventDispatcherMock);
$user->setEnabled(false);
$this->assertEquals(2, $hooksCalled);
$user->setEnabled(true);
}
public function testGetCloudId() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment