Add test for getting unread comment count by folder
Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
parent
429f8ae011
commit
212d9fd277
|
@ -25,9 +25,7 @@
|
|||
namespace OC\Comments;
|
||||
|
||||
use Doctrine\DBAL\Exception\DriverException;
|
||||
use OC\DB\QueryBuilder\Literal;
|
||||
use OC\DB\QueryBuilder\QueryBuilder;
|
||||
use OC\DB\QueryBuilder\QueryFunction;
|
||||
use Doctrine\DBAL\Platforms\MySqlPlatform;
|
||||
use OCP\Comments\CommentsEvent;
|
||||
use OCP\Comments\IComment;
|
||||
use OCP\Comments\ICommentsEventHandler;
|
||||
|
@ -414,13 +412,14 @@ class Manager implements ICommentsManager {
|
|||
*/
|
||||
public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) {
|
||||
$qb = $this->dbConn->getQueryBuilder();
|
||||
$castAs = ($this->dbConn->getDatabasePlatform() instanceof MySqlPlatform) ? 'unsigned integer' : 'int';
|
||||
$query = $qb->select('fileid', $qb->createFunction(
|
||||
'COUNT(' . $qb->getColumnName('c.id') . ')')
|
||||
)->from('comments', 'c')
|
||||
->innerJoin('c', 'filecache', 'f', $qb->expr()->andX(
|
||||
$qb->expr()->eq('c.object_type', $qb->createNamedParameter('files')),
|
||||
$qb->expr()->eq('f.fileid', $qb->createFunction(
|
||||
'cast(' . $qb->getColumnName('c.object_id') . ' as int)'
|
||||
'cast(' . $qb->getColumnName('c.object_id') . ' as ' . $castAs . ')'
|
||||
))
|
||||
))
|
||||
->leftJoin('c', 'comments_read_markers', 'm', $qb->expr()->andX(
|
||||
|
@ -436,7 +435,9 @@ class Manager implements ICommentsManager {
|
|||
->groupBy('f.fileid');
|
||||
|
||||
$resultStatement = $query->execute();
|
||||
return $resultStatement->fetchAll(\PDO::FETCH_KEY_PAIR);
|
||||
return array_map(function ($count) {
|
||||
return (int)$count;
|
||||
}, $resultStatement->fetchAll(\PDO::FETCH_KEY_PAIR));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -6,7 +6,9 @@ use OC\Comments\Comment;
|
|||
use OCP\Comments\CommentsEvent;
|
||||
use OCP\Comments\ICommentsEventHandler;
|
||||
use OCP\Comments\ICommentsManager;
|
||||
use OCP\IDBConnection;
|
||||
use OCP\IUser;
|
||||
use Test\Files\Storage\DummyUser;
|
||||
use Test\TestCase;
|
||||
|
||||
/**
|
||||
|
@ -15,23 +17,30 @@ use Test\TestCase;
|
|||
* @group DB
|
||||
*/
|
||||
class ManagerTest extends TestCase {
|
||||
/** @var IDBConnection */
|
||||
private $connection;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$sql = \OC::$server->getDatabaseConnection()->getDatabasePlatform()->getTruncateTableSQL('`*PREFIX*comments`');
|
||||
\OC::$server->getDatabaseConnection()->prepare($sql)->execute();
|
||||
$this->connection = \OC::$server->getDatabaseConnection();
|
||||
|
||||
$sql = $this->connection->getDatabasePlatform()->getTruncateTableSQL('`*PREFIX*comments`');
|
||||
$this->connection->prepare($sql)->execute();
|
||||
}
|
||||
|
||||
protected function addDatabaseEntry($parentId, $topmostParentId, $creationDT = null, $latestChildDT = null) {
|
||||
protected function addDatabaseEntry($parentId, $topmostParentId, $creationDT = null, $latestChildDT = null, $objectId = null) {
|
||||
if (is_null($creationDT)) {
|
||||
$creationDT = new \DateTime();
|
||||
}
|
||||
if (is_null($latestChildDT)) {
|
||||
$latestChildDT = new \DateTime('yesterday');
|
||||
}
|
||||
if (is_null($objectId)) {
|
||||
$objectId = 'file64';
|
||||
}
|
||||
|
||||
$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
|
||||
$qb = $this->connection->getQueryBuilder();
|
||||
$qb
|
||||
->insert('comments')
|
||||
->values([
|
||||
|
@ -45,7 +54,7 @@ class ManagerTest extends TestCase {
|
|||
'creation_timestamp' => $qb->createNamedParameter($creationDT, 'datetime'),
|
||||
'latest_child_timestamp' => $qb->createNamedParameter($latestChildDT, 'datetime'),
|
||||
'object_type' => $qb->createNamedParameter('files'),
|
||||
'object_id' => $qb->createNamedParameter('file64'),
|
||||
'object_id' => $qb->createNamedParameter($objectId),
|
||||
])
|
||||
->execute();
|
||||
|
||||
|
@ -297,6 +306,52 @@ class ManagerTest extends TestCase {
|
|||
$this->assertSame($amount, 4);
|
||||
}
|
||||
|
||||
public function testGetNumberOfUnreadCommentsForFolder() {
|
||||
// 2 comment for 1111 with 1 before read marker
|
||||
// 2 comments for 1112 with no read marker
|
||||
// 1 comment for 1113 before read marker
|
||||
// 1 comment for 1114 with no read marker
|
||||
$this->addDatabaseEntry(0, 0, null, null, '1112');
|
||||
for ($i = 1; $i < 5; $i++) {
|
||||
$this->addDatabaseEntry(0, 0, null, null, '111' . $i);
|
||||
}
|
||||
$this->addDatabaseEntry(0, 0, (new \DateTime())->modify('-2 days'), null, '1111');
|
||||
$user = $this->createMock(IUser::class);
|
||||
$user->expects($this->any())
|
||||
->method('getUID')
|
||||
->will($this->returnValue('comment_test'));
|
||||
|
||||
$manager = $this->getManager();
|
||||
|
||||
$manager->setReadMark('files', '1111', (new \DateTime())->modify('-1 days'), $user);
|
||||
$manager->setReadMark('files', '1113', (new \DateTime()), $user);
|
||||
|
||||
$query = $this->connection->getQueryBuilder();
|
||||
$query->insert('filecache')
|
||||
->values([
|
||||
'fileid' => $query->createParameter('fileid'),
|
||||
'parent' => $query->createNamedParameter(1000),
|
||||
'size' => $query->createNamedParameter(10),
|
||||
'mtime' => $query->createNamedParameter(10),
|
||||
'storage_mtime' => $query->createNamedParameter(10),
|
||||
'path' => $query->createParameter('path'),
|
||||
'path_hash' => $query->createParameter('path'),
|
||||
]);
|
||||
|
||||
for ($i = 1111; $i < 1115; $i++) {
|
||||
$query->setParameter('path', 'path_' . $i);
|
||||
$query->setParameter('fileid', $i);
|
||||
$query->execute();
|
||||
}
|
||||
|
||||
$amount = $manager->getNumberOfUnreadCommentsForFolder(1000, $user);
|
||||
$this->assertEquals([
|
||||
'1111' => 1,
|
||||
'1112' => 2,
|
||||
'1114' => 1,
|
||||
], $amount);
|
||||
}
|
||||
|
||||
public function invalidCreateArgsProvider() {
|
||||
return [
|
||||
['', 'aId-1', 'oType-1', 'oId-1'],
|
||||
|
@ -643,8 +698,12 @@ class ManagerTest extends TestCase {
|
|||
->method('handle');
|
||||
|
||||
$manager = $this->getManager();
|
||||
$manager->registerEventHandler(function () use ($handler1) {return $handler1; });
|
||||
$manager->registerEventHandler(function () use ($handler2) {return $handler2; });
|
||||
$manager->registerEventHandler(function () use ($handler1) {
|
||||
return $handler1;
|
||||
});
|
||||
$manager->registerEventHandler(function () use ($handler2) {
|
||||
return $handler2;
|
||||
});
|
||||
|
||||
$comment = new Comment();
|
||||
$comment
|
||||
|
@ -724,7 +783,9 @@ class ManagerTest extends TestCase {
|
|||
public function testResolveDisplayNameDirtyResolver() {
|
||||
$manager = $this->getManager();
|
||||
|
||||
$planetClosure = function() { return null; };
|
||||
$planetClosure = function () {
|
||||
return null;
|
||||
};
|
||||
|
||||
$manager->registerDisplayNameResolver('planet', $planetClosure);
|
||||
$this->assertTrue(is_string($manager->resolveDisplayName('planet', 'neptune')));
|
||||
|
@ -736,7 +797,9 @@ class ManagerTest extends TestCase {
|
|||
public function testResolveDisplayNameInvalidType() {
|
||||
$manager = $this->getManager();
|
||||
|
||||
$planetClosure = function() { return null; };
|
||||
$planetClosure = function () {
|
||||
return null;
|
||||
};
|
||||
|
||||
$manager->registerDisplayNameResolver('planet', $planetClosure);
|
||||
$this->assertTrue(is_string($manager->resolveDisplayName(1337, 'neptune')));
|
||||
|
|
Loading…
Reference in New Issue