diff --git a/apps/dav/tests/unit/CalDAV/ResourceBooking/AbstractPrincipalBackendTest.php b/apps/dav/tests/unit/CalDAV/ResourceBooking/AbstractPrincipalBackendTest.php index 19da1782cc..eb9faa574a 100644 --- a/apps/dav/tests/unit/CalDAV/ResourceBooking/AbstractPrincipalBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/ResourceBooking/AbstractPrincipalBackendTest.php @@ -22,7 +22,6 @@ namespace OCA\DAV\Tests\unit\CalDAV\ResourceBooking; use OCP\DB\QueryBuilder\IQueryBuilder; -use OCP\IDBConnection; use OCP\IGroupManager; use OCP\ILogger; use OCP\IUser; @@ -35,9 +34,6 @@ abstract class AbstractPrincipalBackendTest extends TestCase { /** @var \OCA\DAV\CalDAV\ResourceBooking\ResourcePrincipalBackend|\OCA\DAV\CalDAV\ResourceBooking\RoomPrincipalBackend */ protected $principalBackend; - /** @var IDBConnection|\PHPUnit_Framework_MockObject_MockObject */ - protected $dbConnection; - /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */ protected $userSession; @@ -48,7 +44,13 @@ abstract class AbstractPrincipalBackendTest extends TestCase { protected $logger; /** @var string */ - protected $expectedDbTable; + protected $mainDbTable; + + /** @var string */ + protected $metadataDbTable; + + /** @var string */ + protected $foreignKey; /** @var string */ protected $principalPrefix; @@ -59,222 +61,92 @@ abstract class AbstractPrincipalBackendTest extends TestCase { public function setUp() { parent::setUp(); - $this->dbConnection = $this->createMock(IDBConnection::class); $this->userSession = $this->createMock(IUserSession::class); $this->groupManager = $this->createMock(IGroupManager::class); $this->logger = $this->createMock(ILogger::class); } + protected function tearDown() { + $query = self::$realDatabase->getQueryBuilder(); + + $query->delete('calendar_resources')->execute(); + $query->delete('calendar_resources_md')->execute(); + $query->delete('calendar_rooms')->execute(); + $query->delete('calendar_rooms_md')->execute(); + } + public function testGetPrincipalsByPrefix() { - $queryBuilder = $this->createMock(IQueryBuilder::class); - $stmt = $this->createMock(\Doctrine\DBAL\Driver\Statement::class); - $this->dbConnection->expects($this->once()) - ->method('getQueryBuilder') - ->with() - ->will($this->returnValue($queryBuilder)); - - $queryBuilder->expects($this->at(0)) - ->method('select') - ->with(['id', 'backend_id', 'resource_id', 'email', 'displayname']) - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(1)) - ->method('from') - ->with($this->expectedDbTable) - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(2)) - ->method('execute') - ->with() - ->will($this->returnValue($stmt)); - - $stmt->expects($this->at(0)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue([ - 'id' => 0, - 'backend_id' => 'db', - 'resource_id' => '123', - 'email' => 'foo@bar.com', - 'displayname' => 'Resource 123' - ])); - $stmt->expects($this->at(1)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue([ - 'id' => 1, - 'backend_id' => 'ldap', - 'resource_id' => '123', - 'email' => 'ldap@bar.com', - 'displayname' => 'Resource 123 ldap' - ])); - $stmt->expects($this->at(2)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue([ - 'id' => 2, - 'backend_id' => 'db', - 'resource_id' => '456', - 'email' => 'bli@bar.com', - 'displayname' => 'Resource 456' - ])); - $stmt->expects($this->at(3)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue(null)); - $stmt->expects($this->at(4)) - ->method('closeCursor') - ->with(); - $actual = $this->principalBackend->getPrincipalsByPrefix($this->principalPrefix); + $this->assertEquals([ [ - 'uri' => $this->principalPrefix . '/db-123', - '{DAV:}displayname' => 'Resource 123', - '{http://sabredav.org/ns}email-address' => 'foo@bar.com', + 'uri' => $this->principalPrefix . '/backend1-res1', + '{DAV:}displayname' => 'Beamer1', + '{http://sabredav.org/ns}email-address' => 'res1@foo.bar', '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => $this->expectedCUType, ], [ - 'uri' => $this->principalPrefix . '/ldap-123', - '{DAV:}displayname' => 'Resource 123 ldap', - '{http://sabredav.org/ns}email-address' => 'ldap@bar.com', + 'uri' => $this->principalPrefix . '/backend1-res2', + '{DAV:}displayname' => 'TV1', + '{http://sabredav.org/ns}email-address' => 'res2@foo.bar', '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => $this->expectedCUType, ], [ - 'uri' => $this->principalPrefix . '/db-456', - '{DAV:}displayname' => 'Resource 456', - '{http://sabredav.org/ns}email-address' => 'bli@bar.com', + 'uri' => $this->principalPrefix . '/backend2-res3', + '{DAV:}displayname' => 'Beamer2', + '{http://sabredav.org/ns}email-address' => 'res3@foo.bar', + '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => $this->expectedCUType, + '{http://nextcloud.com/ns}foo' => 'value1', + '{http://nextcloud.com/ns}meta2' => 'value2', + ], + [ + 'uri' => $this->principalPrefix . '/backend2-res4', + '{DAV:}displayname' => 'TV2', + '{http://sabredav.org/ns}email-address' => 'res4@foo.bar', + '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => $this->expectedCUType, + '{http://nextcloud.com/ns}meta1' => 'value1', + '{http://nextcloud.com/ns}meta3' => 'value3-old', + ], + [ + 'uri' => $this->principalPrefix . '/backend3-res5', + '{DAV:}displayname' => 'Beamer3', + '{http://sabredav.org/ns}email-address' => 'res5@foo.bar', '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => $this->expectedCUType, ], + [ + 'uri' => $this->principalPrefix . '/backend3-res6', + '{DAV:}displayname' => 'Pointer', + '{http://sabredav.org/ns}email-address' => 'res6@foo.bar', + '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => $this->expectedCUType, + '{http://nextcloud.com/ns}meta99' => 'value99' + ] ], $actual); } public function testGetNoPrincipalsByPrefixForWrongPrincipalPrefix() { - $this->dbConnection->expects($this->never()) - ->method('getQueryBuilder'); - $actual = $this->principalBackend->getPrincipalsByPrefix('principals/users'); $this->assertEquals([], $actual); } public function testGetPrincipalByPath() { - $queryBuilder = $this->createMock(IQueryBuilder::class); - $stmt = $this->createMock(\Doctrine\DBAL\Driver\Statement::class); - $expr = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class); - - $this->dbConnection->expects($this->at(0)) - ->method('getQueryBuilder') - ->with() - ->will($this->returnValue($queryBuilder)); - $queryBuilder->method('expr') - ->will($this->returnValue($expr)); - $expr->method('eq') - ->will($this->returnValueMap([ - ['backend_id', 'createNamedParameter-1', null, 'WHERE_CLAUSE_1'], - ['resource_id', 'createNamedParameter-2', null, 'WHERE_CLAUSE_2'], - ])); - $queryBuilder->method('createNamedParameter') - ->will($this->returnValueMap([ - ['db', \PDO::PARAM_STR, null, 'createNamedParameter-1'], - ['123', \PDO::PARAM_STR, null, 'createNamedParameter-2'], - ])); - - $queryBuilder->expects($this->at(0)) - ->method('select') - ->with(['id', 'backend_id', 'resource_id', 'email', 'displayname']) - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(1)) - ->method('from') - ->with($this->expectedDbTable) - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(4)) - ->method('where') - ->with('WHERE_CLAUSE_1') - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(7)) - ->method('andWhere') - ->with('WHERE_CLAUSE_2') - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(8)) - ->method('execute') - ->with() - ->will($this->returnValue($stmt)); - - $stmt->expects($this->at(0)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue([ - 'id' => 0, - 'backend_id' => 'db', - 'resource_id' => '123', - 'email' => 'foo@bar.com', - 'displayname' => 'Resource 123' - ])); - - $actual = $this->principalBackend->getPrincipalByPath($this->principalPrefix . '/db-123'); + $actual = $this->principalBackend->getPrincipalByPath($this->principalPrefix . '/backend2-res3'); $this->assertEquals([ - 'uri' => $this->principalPrefix . '/db-123', - '{DAV:}displayname' => 'Resource 123', - '{http://sabredav.org/ns}email-address' => 'foo@bar.com', + 'uri' => $this->principalPrefix . '/backend2-res3', + '{DAV:}displayname' => 'Beamer2', + '{http://sabredav.org/ns}email-address' => 'res3@foo.bar', '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => $this->expectedCUType, + '{http://nextcloud.com/ns}foo' => 'value1', + '{http://nextcloud.com/ns}meta2' => 'value2', ], $actual); } public function testGetPrincipalByPathNotFound() { - $queryBuilder = $this->createMock(IQueryBuilder::class); - $stmt = $this->createMock(\Doctrine\DBAL\Driver\Statement::class); - $expr = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class); - - $this->dbConnection->expects($this->at(0)) - ->method('getQueryBuilder') - ->with() - ->will($this->returnValue($queryBuilder)); - $queryBuilder->method('expr') - ->will($this->returnValue($expr)); - $expr->method('eq') - ->will($this->returnValueMap([ - ['backend_id', 'createNamedParameter-1', null, 'WHERE_CLAUSE_1'], - ['resource_id', 'createNamedParameter-2', null, 'WHERE_CLAUSE_2'], - ])); - $queryBuilder->method('createNamedParameter') - ->will($this->returnValueMap([ - ['db', \PDO::PARAM_STR, null, 'createNamedParameter-1'], - ['123', \PDO::PARAM_STR, null, 'createNamedParameter-2'], - ])); - - $queryBuilder->expects($this->at(0)) - ->method('select') - ->with(['id', 'backend_id', 'resource_id', 'email', 'displayname']) - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(1)) - ->method('from') - ->with($this->expectedDbTable) - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(4)) - ->method('where') - ->with('WHERE_CLAUSE_1') - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(7)) - ->method('andWhere') - ->with('WHERE_CLAUSE_2') - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(8)) - ->method('execute') - ->with() - ->will($this->returnValue($stmt)); - - $stmt->expects($this->at(0)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue(false)); - $actual = $this->principalBackend->getPrincipalByPath($this->principalPrefix . '/db-123'); $this->assertEquals(null, $actual); } public function testGetPrincipalByPathWrongPrefix() { - $this->dbConnection->expects($this->never()) - ->method('getQueryBuilder'); - $actual = $this->principalBackend->getPrincipalByPath('principals/users/foo-bar'); $this->assertEquals(null, $actual); } @@ -318,182 +190,9 @@ abstract class AbstractPrincipalBackendTest extends TestCase { ->with($user) ->will($this->returnValue(['group1', 'group2'])); - $queryBuilder1 = $this->createMock(IQueryBuilder::class); - $queryBuilder2 = $this->createMock(IQueryBuilder::class); - $stmt1 = $this->createMock(\Doctrine\DBAL\Driver\Statement::class); - $stmt2 = $this->createMock(\Doctrine\DBAL\Driver\Statement::class); - $expr1 = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class); - $expr2 = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class); - - $this->dbConnection->expects($this->at(0)) - ->method('getQueryBuilder') - ->with() - ->will($this->returnValue($queryBuilder1)); - $this->dbConnection->expects($this->at(1)) - ->method('escapeLikeParameter') - ->with('foo') - ->will($this->returnValue('escapedFoo')); - $this->dbConnection->expects($this->at(2)) - ->method('getQueryBuilder') - ->with() - ->will($this->returnValue($queryBuilder2)); - $this->dbConnection->expects($this->at(3)) - ->method('escapeLikeParameter') - ->with('bar') - ->will($this->returnValue('escapedBar')); - - $queryBuilder1->method('expr') - ->will($this->returnValue($expr1)); - $queryBuilder2->method('expr') - ->will($this->returnValue($expr2)); - - $expr1->method('iLike') - ->will($this->returnValueMap([ - ['email', 'createNamedParameter-1', null, 'ILIKE_CLAUSE_1'], - ])); - $expr2->method('iLike') - ->will($this->returnValueMap([ - ['displayname', 'createNamedParameter-2', null, 'ILIKE_CLAUSE_2'], - ])); - - $queryBuilder1->method('expr') - ->will($this->returnValue($expr1)); - $queryBuilder2->method('expr') - ->will($this->returnValue($expr2)); - - $queryBuilder1->method('createNamedParameter') - ->will($this->returnValueMap([ - ['%escapedFoo%', \PDO::PARAM_STR, null, 'createNamedParameter-1'], - ])); - $queryBuilder2->method('createNamedParameter') - ->will($this->returnValueMap([ - ['%escapedBar%', \PDO::PARAM_STR, null, 'createNamedParameter-2'], - ])); - - $queryBuilder1->expects($this->at(0)) - ->method('select') - ->with(['id', 'backend_id', 'resource_id', 'email', 'displayname', 'group_restrictions']) - ->will($this->returnValue($queryBuilder1)); - $queryBuilder1->expects($this->at(1)) - ->method('from') - ->with($this->expectedDbTable) - ->will($this->returnValue($queryBuilder1)); - $queryBuilder1->expects($this->at(4)) - ->method('where') - ->with('ILIKE_CLAUSE_1') - ->will($this->returnValue($queryBuilder1)); - $queryBuilder1->expects($this->at(5)) - ->method('execute') - ->with() - ->will($this->returnValue($stmt1)); - - $queryBuilder2->expects($this->at(0)) - ->method('select') - ->with(['id', 'backend_id', 'resource_id', 'email', 'displayname', 'group_restrictions']) - ->will($this->returnValue($queryBuilder2)); - $queryBuilder2->expects($this->at(1)) - ->method('from') - ->with($this->expectedDbTable) - ->will($this->returnValue($queryBuilder2)); - $queryBuilder2->expects($this->at(4)) - ->method('where') - ->with('ILIKE_CLAUSE_2') - ->will($this->returnValue($queryBuilder2)); - $queryBuilder2->expects($this->at(5)) - ->method('execute') - ->with() - ->will($this->returnValue($stmt2)); - - $stmt1->expects($this->at(0)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue([ - 'id' => 0, - 'backend_id' => 'db', - 'resource_id' => '1', - 'email' => '1', - 'displayname' => 'Resource 1', - 'group_restrictions' => null, - ])); - $stmt1->expects($this->at(1)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue([ - 'id' => 1, - 'backend_id' => 'db', - 'resource_id' => '2', - 'email' => '2', - 'displayname' => 'Resource 2', - 'group_restrictions' => '', - ])); - $stmt1->expects($this->at(2)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue([ - 'id' => 2, - 'backend_id' => 'db', - 'resource_id' => '3', - 'email' => '3', - 'displayname' => 'Resource 3', - 'group_restrictions' => '["group3"]', - ])); - $stmt1->expects($this->at(3)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue([ - 'id' => 99, - 'backend_id' => 'db', - 'resource_id' => '99', - 'email' => '99', - 'displayname' => 'Resource 99', - 'group_restrictions' => '["group1", "group2"]', - ])); - $stmt1->expects($this->at(4)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue(null)); - - $stmt2->expects($this->at(0)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue([ - 'id' => 0, - 'backend_id' => 'db', - 'resource_id' => '4', - 'email' => '4', - 'displayname' => 'Resource 4', - 'group_restrictions' => '[]' - ])); - $stmt2->expects($this->at(1)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue([ - 'id' => 1, - 'backend_id' => 'db', - 'resource_id' => '5', - 'email' => '5', - 'displayname' => 'Resource 5', - 'group_restrictions' => '["group1", "group5"]' - ])); - $stmt2->expects($this->at(2)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue([ - 'id' => 99, - 'backend_id' => 'db', - 'resource_id' => '99', - 'email' => '99', - 'displayname' => 'Resource 99', - 'group_restrictions' => '["group1", "group2"]', - ])); - $stmt2->expects($this->at(3)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue(null)); - $actual = $this->principalBackend->searchPrincipals($this->principalPrefix, [ '{http://sabredav.org/ns}email-address' => 'foo', - '{DAV:}displayname' => 'bar', + '{DAV:}displayname' => 'Beamer', ], $test); $this->assertEquals( @@ -507,133 +206,35 @@ abstract class AbstractPrincipalBackendTest extends TestCase { // at that point, so we need this hack return [ [[ - '%prefix%/db-99' + '%prefix%/backend1-res1', + '%prefix%/backend2-res3', ], 'allof'], [[ - '%prefix%/db-1', - '%prefix%/db-2', - '%prefix%/db-99', - '%prefix%/db-4', - '%prefix%/db-5', + '%prefix%/backend1-res1', + '%prefix%/backend1-res2', + '%prefix%/backend2-res3', + '%prefix%/backend2-res4', + '%prefix%/backend3-res6', ], 'anyof'], ]; } public function testSearchPrincipalsByCalendarUserAddressSet() { $user = $this->createMock(IUser::class); - $this->userSession->expects($this->exactly(2)) - ->method('getUser') + $this->userSession->method('getUser') ->with() ->will($this->returnValue($user)); - $this->groupManager->expects($this->exactly(2)) - ->method('getUserGroupIds') + $this->groupManager->method('getUserGroupIds') ->with($user) ->will($this->returnValue(['group1', 'group2'])); - $queryBuilder1 = $this->createMock(IQueryBuilder::class); - $stmt1 = $this->createMock(\Doctrine\DBAL\Driver\Statement::class); - $expr1 = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class); - - $this->dbConnection->expects($this->at(0)) - ->method('getQueryBuilder') - ->with() - ->will($this->returnValue($queryBuilder1)); - $this->dbConnection->expects($this->at(1)) - ->method('escapeLikeParameter') - ->with('foo') - ->will($this->returnValue('escapedFoo')); - - $queryBuilder1->method('expr') - ->will($this->returnValue($expr1)); - - $expr1->method('iLike') - ->will($this->returnValueMap([ - ['email', 'createNamedParameter-1', null, 'ILIKE_CLAUSE_1'], - ])); - - $queryBuilder1->method('expr') - ->will($this->returnValue($expr1)); - - $queryBuilder1->method('createNamedParameter') - ->will($this->returnValueMap([ - ['%escapedFoo%', \PDO::PARAM_STR, null, 'createNamedParameter-1'], - ])); - - $queryBuilder1->expects($this->at(0)) - ->method('select') - ->with(['id', 'backend_id', 'resource_id', 'email', 'displayname', 'group_restrictions']) - ->will($this->returnValue($queryBuilder1)); - $queryBuilder1->expects($this->at(1)) - ->method('from') - ->with($this->expectedDbTable) - ->will($this->returnValue($queryBuilder1)); - $queryBuilder1->expects($this->at(4)) - ->method('where') - ->with('ILIKE_CLAUSE_1') - ->will($this->returnValue($queryBuilder1)); - $queryBuilder1->expects($this->at(5)) - ->method('execute') - ->with() - ->will($this->returnValue($stmt1)); - - $stmt1->expects($this->at(0)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue([ - 'id' => 0, - 'backend_id' => 'db', - 'resource_id' => '1', - 'email' => '1', - 'displayname' => 'Resource 1', - 'group_restrictions' => null, - ])); - $stmt1->expects($this->at(1)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue([ - 'id' => 1, - 'backend_id' => 'db', - 'resource_id' => '2', - 'email' => '2', - 'displayname' => 'Resource 2', - 'group_restrictions' => '', - ])); - $stmt1->expects($this->at(2)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue([ - 'id' => 2, - 'backend_id' => 'db', - 'resource_id' => '3', - 'email' => '3', - 'displayname' => 'Resource 3', - 'group_restrictions' => '["group3"]', - ])); - $stmt1->expects($this->at(3)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue([ - 'id' => 99, - 'backend_id' => 'db', - 'resource_id' => '99', - 'email' => '99', - 'displayname' => 'Resource 99', - 'group_restrictions' => '["group1", "group2"]', - ])); - $stmt1->expects($this->at(4)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue(null)); - $actual = $this->principalBackend->searchPrincipals($this->principalPrefix, [ - '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set' => 'foo', + '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set' => 'res2@foo.bar', ]); $this->assertEquals( str_replace('%prefix%', $this->principalPrefix, [ - '%prefix%/db-1', - '%prefix%/db-2', - '%prefix%/db-99', + '%prefix%/backend1-res2', ]), $actual); } @@ -643,8 +244,6 @@ abstract class AbstractPrincipalBackendTest extends TestCase { ->method('getUser'); $this->groupManager->expects($this->never()) ->method('getUserGroupIds'); - $this->dbConnection->expects($this->never()) - ->method('getQueryBuilder'); $this->principalBackend->searchPrincipals($this->principalPrefix, []); } @@ -654,8 +253,6 @@ abstract class AbstractPrincipalBackendTest extends TestCase { ->method('getUser'); $this->groupManager->expects($this->never()) ->method('getUserGroupIds'); - $this->dbConnection->expects($this->never()) - ->method('getQueryBuilder'); $this->principalBackend->searchPrincipals('principals/users', [ '{http://sabredav.org/ns}email-address' => 'foo' @@ -673,56 +270,8 @@ abstract class AbstractPrincipalBackendTest extends TestCase { ->with($user) ->will($this->returnValue(['group1', 'group2'])); - $queryBuilder = $this->createMock(IQueryBuilder::class); - $stmt = $this->createMock(\Doctrine\DBAL\Driver\Statement::class); - $expr = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class); - - $this->dbConnection->expects($this->at(0)) - ->method('getQueryBuilder') - ->with() - ->will($this->returnValue($queryBuilder)); - $queryBuilder->method('expr') - ->will($this->returnValue($expr)); - $expr->method('eq') - ->will($this->returnValueMap([ - ['email', 'createNamedParameter-1', null, 'WHERE_CLAUSE_1'], - ])); - $queryBuilder->method('createNamedParameter') - ->will($this->returnValueMap([ - ['foo@bar.com', \PDO::PARAM_STR, null, 'createNamedParameter-1'], - ])); - - $queryBuilder->expects($this->at(0)) - ->method('select') - ->with(['id', 'backend_id', 'resource_id', 'email', 'displayname', 'group_restrictions']) - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(1)) - ->method('from') - ->with($this->expectedDbTable) - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(4)) - ->method('where') - ->with('WHERE_CLAUSE_1') - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(5)) - ->method('execute') - ->with() - ->will($this->returnValue($stmt)); - - $stmt->expects($this->at(0)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue([ - 'id' => 0, - 'backend_id' => 'db', - 'resource_id' => '123', - 'email' => 'foo@bar.com', - 'displayname' => 'Resource 123', - 'group_restrictions' => '["group1"]', - ])); - - $actual = $this->principalBackend->findByUri('mailto:foo@bar.com', $this->principalPrefix); - $this->assertEquals($this->principalPrefix . '/db-123', $actual); + $actual = $this->principalBackend->findByUri('mailto:res1@foo.bar', $this->principalPrefix); + $this->assertEquals($this->principalPrefix . '/backend1-res1', $actual); } public function testFindByUriByEmailForbiddenResource() { @@ -736,55 +285,7 @@ abstract class AbstractPrincipalBackendTest extends TestCase { ->with($user) ->will($this->returnValue(['group1', 'group2'])); - $queryBuilder = $this->createMock(IQueryBuilder::class); - $stmt = $this->createMock(\Doctrine\DBAL\Driver\Statement::class); - $expr = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class); - - $this->dbConnection->expects($this->at(0)) - ->method('getQueryBuilder') - ->with() - ->will($this->returnValue($queryBuilder)); - $queryBuilder->method('expr') - ->will($this->returnValue($expr)); - $expr->method('eq') - ->will($this->returnValueMap([ - ['email', 'createNamedParameter-1', null, 'WHERE_CLAUSE_1'], - ])); - $queryBuilder->method('createNamedParameter') - ->will($this->returnValueMap([ - ['foo@bar.com', \PDO::PARAM_STR, null, 'createNamedParameter-1'], - ])); - - $queryBuilder->expects($this->at(0)) - ->method('select') - ->with(['id', 'backend_id', 'resource_id', 'email', 'displayname', 'group_restrictions']) - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(1)) - ->method('from') - ->with($this->expectedDbTable) - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(4)) - ->method('where') - ->with('WHERE_CLAUSE_1') - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(5)) - ->method('execute') - ->with() - ->will($this->returnValue($stmt)); - - $stmt->expects($this->at(0)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue([ - 'id' => 0, - 'backend_id' => 'db', - 'resource_id' => '123', - 'email' => 'foo@bar.com', - 'displayname' => 'Resource 123', - 'group_restrictions' => '["group3"]', - ])); - - $actual = $this->principalBackend->findByUri('mailto:foo@bar.com', $this->principalPrefix); + $actual = $this->principalBackend->findByUri('mailto:res5@foo.bar', $this->principalPrefix); $this->assertEquals(null, $actual); } @@ -799,48 +300,7 @@ abstract class AbstractPrincipalBackendTest extends TestCase { ->with($user) ->will($this->returnValue(['group1', 'group2'])); - $queryBuilder = $this->createMock(IQueryBuilder::class); - $stmt = $this->createMock(\Doctrine\DBAL\Driver\Statement::class); - $expr = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class); - - $this->dbConnection->expects($this->at(0)) - ->method('getQueryBuilder') - ->with() - ->will($this->returnValue($queryBuilder)); - $queryBuilder->method('expr') - ->will($this->returnValue($expr)); - $expr->method('eq') - ->will($this->returnValueMap([ - ['email', 'createNamedParameter-1', null, 'WHERE_CLAUSE_1'], - ])); - $queryBuilder->method('createNamedParameter') - ->will($this->returnValueMap([ - ['foo@bar.com', \PDO::PARAM_STR, null, 'createNamedParameter-1'], - ])); - - $queryBuilder->expects($this->at(0)) - ->method('select') - ->with(['id', 'backend_id', 'resource_id', 'email', 'displayname', 'group_restrictions']) - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(1)) - ->method('from') - ->with($this->expectedDbTable) - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(4)) - ->method('where') - ->with('WHERE_CLAUSE_1') - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(5)) - ->method('execute') - ->with() - ->will($this->returnValue($stmt)); - - $stmt->expects($this->at(0)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue(null)); - - $actual = $this->principalBackend->findByUri('mailto:foo@bar.com', $this->principalPrefix); + $actual = $this->principalBackend->findByUri('mailto:res99@foo.bar', $this->principalPrefix); $this->assertEquals(null, $actual); } @@ -855,56 +315,8 @@ abstract class AbstractPrincipalBackendTest extends TestCase { ->with($user) ->will($this->returnValue(['group1', 'group2'])); - $queryBuilder = $this->createMock(IQueryBuilder::class); - $stmt = $this->createMock(\Doctrine\DBAL\Driver\Statement::class); - $expr = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class); - - $this->dbConnection->expects($this->at(0)) - ->method('getQueryBuilder') - ->with() - ->will($this->returnValue($queryBuilder)); - $queryBuilder->method('expr') - ->will($this->returnValue($expr)); - $expr->method('eq') - ->will($this->returnValueMap([ - ['email', 'createNamedParameter-1', null, 'WHERE_CLAUSE_1'], - ])); - $queryBuilder->method('createNamedParameter') - ->will($this->returnValueMap([ - ['foo@bar.com', \PDO::PARAM_STR, null, 'createNamedParameter-1'], - ])); - - $queryBuilder->expects($this->at(0)) - ->method('select') - ->with(['id', 'backend_id', 'resource_id', 'email', 'displayname', 'group_restrictions']) - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(1)) - ->method('from') - ->with($this->expectedDbTable) - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(4)) - ->method('where') - ->with('WHERE_CLAUSE_1') - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(5)) - ->method('execute') - ->with() - ->will($this->returnValue($stmt)); - - $stmt->expects($this->at(0)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue([ - 'id' => 0, - 'backend_id' => 'db', - 'resource_id' => '123', - 'email' => 'foo@bar.com', - 'displayname' => 'Resource 123', - 'group_restrictions' => '["group1"]', - ])); - - $actual = $this->principalBackend->findByUri('mailto:foo@bar.com', $this->principalPrefix); - $this->assertEquals($this->principalPrefix . '/db-123', $actual); + $actual = $this->principalBackend->findByUri('mailto:res6@foo.bar', $this->principalPrefix); + $this->assertEquals($this->principalPrefix . '/backend3-res6', $actual); } public function testFindByUriByPrincipalForbiddenResource() { @@ -918,61 +330,7 @@ abstract class AbstractPrincipalBackendTest extends TestCase { ->with($user) ->will($this->returnValue(['group1', 'group2'])); - $queryBuilder = $this->createMock(IQueryBuilder::class); - $stmt = $this->createMock(\Doctrine\DBAL\Driver\Statement::class); - $expr = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class); - - $this->dbConnection->expects($this->at(0)) - ->method('getQueryBuilder') - ->with() - ->will($this->returnValue($queryBuilder)); - $queryBuilder->method('expr') - ->will($this->returnValue($expr)); - $expr->method('eq') - ->will($this->returnValueMap([ - ['backend_id', 'createNamedParameter-1', null, 'WHERE_CLAUSE_1'], - ['resource_id', 'createNamedParameter-2', null, 'WHERE_CLAUSE_2'], - ])); - $queryBuilder->method('createNamedParameter') - ->will($this->returnValueMap([ - ['db', \PDO::PARAM_STR, null, 'createNamedParameter-1'], - ['123', \PDO::PARAM_STR, null, 'createNamedParameter-2'], - ])); - - $queryBuilder->expects($this->at(0)) - ->method('select') - ->with(['id', 'backend_id', 'resource_id', 'email', 'displayname', 'group_restrictions']) - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(1)) - ->method('from') - ->with($this->expectedDbTable) - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(4)) - ->method('where') - ->with('WHERE_CLAUSE_1') - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(7)) - ->method('andWhere') - ->with('WHERE_CLAUSE_2') - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(8)) - ->method('execute') - ->with() - ->will($this->returnValue($stmt)); - - $stmt->expects($this->at(0)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue([ - 'id' => 0, - 'backend_id' => 'db', - 'resource_id' => '123', - 'email' => 'foo@bar.com', - 'displayname' => 'Resource 123', - 'group_restrictions' => '["group3"]', - ])); - - $actual = $this->principalBackend->findByUri('principal:' . $this->principalPrefix . '/db-123', $this->principalPrefix); + $actual = $this->principalBackend->findByUri('principal:' . $this->principalPrefix . '/backend3-res5', $this->principalPrefix); $this->assertEquals(null, $actual); } @@ -987,53 +345,6 @@ abstract class AbstractPrincipalBackendTest extends TestCase { ->with($user) ->will($this->returnValue(['group1', 'group2'])); - $queryBuilder = $this->createMock(IQueryBuilder::class); - $stmt = $this->createMock(\Doctrine\DBAL\Driver\Statement::class); - $expr = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class); - - $this->dbConnection->expects($this->at(0)) - ->method('getQueryBuilder') - ->with() - ->will($this->returnValue($queryBuilder)); - $queryBuilder->method('expr') - ->will($this->returnValue($expr)); - $expr->method('eq') - ->will($this->returnValueMap([ - ['backend_id', 'createNamedParameter-1', null, 'WHERE_CLAUSE_1'], - ['resource_id', 'createNamedParameter-2', null, 'WHERE_CLAUSE_2'], - ])); - $queryBuilder->method('createNamedParameter') - ->will($this->returnValueMap([ - ['db', \PDO::PARAM_STR, null, 'createNamedParameter-1'], - ['123', \PDO::PARAM_STR, null, 'createNamedParameter-2'], - ])); - - $queryBuilder->expects($this->at(0)) - ->method('select') - ->with(['id', 'backend_id', 'resource_id', 'email', 'displayname', 'group_restrictions']) - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(1)) - ->method('from') - ->with($this->expectedDbTable) - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(4)) - ->method('where') - ->with('WHERE_CLAUSE_1') - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(7)) - ->method('andWhere') - ->with('WHERE_CLAUSE_2') - ->will($this->returnValue($queryBuilder)); - $queryBuilder->expects($this->at(8)) - ->method('execute') - ->with() - ->will($this->returnValue($stmt)); - - $stmt->expects($this->at(0)) - ->method('fetch') - ->with(\PDO::FETCH_ASSOC) - ->will($this->returnValue(null)); - $actual = $this->principalBackend->findByUri('principal:' . $this->principalPrefix . '/db-123', $this->principalPrefix); $this->assertEquals(null, $actual); } @@ -1053,4 +364,106 @@ abstract class AbstractPrincipalBackendTest extends TestCase { $this->assertEquals(null, $actual); } + protected function createTestDatasetInDb() { + $query = self::$realDatabase->getQueryBuilder(); + $query->insert($this->mainDbTable) + ->values([ + 'backend_id' => $query->createNamedParameter('backend1'), + 'resource_id' => $query->createNamedParameter('res1'), + 'email' => $query->createNamedParameter('res1@foo.bar'), + 'displayname' => $query->createNamedParameter('Beamer1'), + 'group_restrictions' => $query->createNamedParameter('[]'), + ]) + ->execute(); + + $query->insert($this->mainDbTable) + ->values([ + 'backend_id' => $query->createNamedParameter('backend1'), + 'resource_id' => $query->createNamedParameter('res2'), + 'email' => $query->createNamedParameter('res2@foo.bar'), + 'displayname' => $query->createNamedParameter('TV1'), + 'group_restrictions' => $query->createNamedParameter('[]'), + ]) + ->execute(); + + $query->insert($this->mainDbTable) + ->values([ + 'backend_id' => $query->createNamedParameter('backend2'), + 'resource_id' => $query->createNamedParameter('res3'), + 'email' => $query->createNamedParameter('res3@foo.bar'), + 'displayname' => $query->createNamedParameter('Beamer2'), + 'group_restrictions' => $query->createNamedParameter('[]'), + ]) + ->execute(); + $id3 = $query->getLastInsertId(); + + $query->insert($this->mainDbTable) + ->values([ + 'backend_id' => $query->createNamedParameter('backend2'), + 'resource_id' => $query->createNamedParameter('res4'), + 'email' => $query->createNamedParameter('res4@foo.bar'), + 'displayname' => $query->createNamedParameter('TV2'), + 'group_restrictions' => $query->createNamedParameter('[]'), + ]) + ->execute(); + $id4 = $query->getLastInsertId(); + + $query->insert($this->mainDbTable) + ->values([ + 'backend_id' => $query->createNamedParameter('backend3'), + 'resource_id' => $query->createNamedParameter('res5'), + 'email' => $query->createNamedParameter('res5@foo.bar'), + 'displayname' => $query->createNamedParameter('Beamer3'), + 'group_restrictions' => $query->createNamedParameter('["foo", "bar"]'), + ]) + ->execute(); + + $query->insert($this->mainDbTable) + ->values([ + 'backend_id' => $query->createNamedParameter('backend3'), + 'resource_id' => $query->createNamedParameter('res6'), + 'email' => $query->createNamedParameter('res6@foo.bar'), + 'displayname' => $query->createNamedParameter('Pointer'), + 'group_restrictions' => $query->createNamedParameter('["group1", "bar"]'), + ]) + ->execute(); + $id6 = $query->getLastInsertId(); + + $query->insert($this->metadataDbTable) + ->values([ + $this->foreignKey => $query->createNamedParameter($id3), + 'key' => $query->createNamedParameter('{http://nextcloud.com/ns}foo'), + 'value' => $query->createNamedParameter('value1') + ]) + ->execute(); + $query->insert($this->metadataDbTable) + ->values([ + $this->foreignKey => $query->createNamedParameter($id3), + 'key' => $query->createNamedParameter('{http://nextcloud.com/ns}meta2'), + 'value' => $query->createNamedParameter('value2') + ]) + ->execute(); + $query->insert($this->metadataDbTable) + ->values([ + $this->foreignKey => $query->createNamedParameter($id4), + 'key' => $query->createNamedParameter('{http://nextcloud.com/ns}meta1'), + 'value' => $query->createNamedParameter('value1') + ]) + ->execute(); + $query->insert($this->metadataDbTable) + ->values([ + $this->foreignKey => $query->createNamedParameter($id4), + 'key' => $query->createNamedParameter('{http://nextcloud.com/ns}meta3'), + 'value' => $query->createNamedParameter('value3-old') + ]) + ->execute(); + $query->insert($this->metadataDbTable) + ->values([ + $this->foreignKey => $query->createNamedParameter($id6), + 'key' => $query->createNamedParameter('{http://nextcloud.com/ns}meta99'), + 'value' => $query->createNamedParameter('value99') + ]) + ->execute(); + } + } diff --git a/apps/dav/tests/unit/CalDAV/ResourceBooking/ResourcePrincipalBackendTest.php b/apps/dav/tests/unit/CalDAV/ResourceBooking/ResourcePrincipalBackendTest.php index d3c774417c..3787e4df95 100644 --- a/apps/dav/tests/unit/CalDAV/ResourceBooking/ResourcePrincipalBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/ResourceBooking/ResourcePrincipalBackendTest.php @@ -27,10 +27,16 @@ Class ResourcePrincipalBackendTest extends AbstractPrincipalBackendTest { public function setUp() { parent::setUp(); - $this->principalBackend = new ResourcePrincipalBackend($this->dbConnection, + $this->principalBackend = new ResourcePrincipalBackend(self::$realDatabase, $this->userSession, $this->groupManager, $this->logger); - $this->expectedDbTable = 'calendar_resources'; + + $this->mainDbTable = 'calendar_resources'; + $this->metadataDbTable = 'calendar_resources_md'; + $this->foreignKey = 'resource_id'; + $this->principalPrefix = 'principals/calendar-resources'; $this->expectedCUType = 'RESOURCE'; + + $this->createTestDatasetInDb(); } } diff --git a/apps/dav/tests/unit/CalDAV/ResourceBooking/RoomPrincipalBackendTest.php b/apps/dav/tests/unit/CalDAV/ResourceBooking/RoomPrincipalBackendTest.php index a10aaa26b3..9b259c30ad 100644 --- a/apps/dav/tests/unit/CalDAV/ResourceBooking/RoomPrincipalBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/ResourceBooking/RoomPrincipalBackendTest.php @@ -27,10 +27,16 @@ Class RoomPrincipalBackendTest extends AbstractPrincipalBackendTest { public function setUp() { parent::setUp(); - $this->principalBackend = new RoomPrincipalBackend($this->dbConnection, + $this->principalBackend = new RoomPrincipalBackend(self::$realDatabase, $this->userSession, $this->groupManager, $this->logger); - $this->expectedDbTable = 'calendar_rooms'; + + $this->mainDbTable = 'calendar_rooms'; + $this->metadataDbTable = 'calendar_rooms_md'; + $this->foreignKey = 'room_id'; + $this->principalPrefix = 'principals/calendar-rooms'; $this->expectedCUType = 'ROOM'; + + $this->createTestDatasetInDb(); } }