Browse Source

Fix "Object of class DateTime could not be converted to int"

Signed-off-by: Joas Schilling <coding@schilljs.com>
pull/1134/head
Joas Schilling 7 years ago
parent
commit
22c9e0eba3
No known key found for this signature in database GPG Key ID: 7076EA9751AACDDA
  1. 9
      lib/Chat/AutoComplete/Sorter.php
  2. 96
      tests/php/Chat/AutoComplete/SorterTest.php

9
lib/Chat/AutoComplete/Sorter.php

@ -38,7 +38,7 @@ class Sorter implements ISorter {
* @return string The ID of the sorter, e.g. commenters
* @since 13.0.0
*/
public function getId() {
public function getId(): string {
return 'talk_chat_participants';
}
@ -51,6 +51,11 @@ class Sorter implements ISorter {
*/
public function sort(array &$sortArray, array $context) {
foreach ($sortArray as $type => &$byType) {
if ($type !== 'users') {
continue;
}
/** @var \DateTime[] $lastComments */
$lastComments = $this->commentsManager->getLastCommentDateByActor(
$context['itemType'],
$context['itemId'],
@ -67,7 +72,7 @@ class Sorter implements ISorter {
if (!isset($lastComments[$a['value']['shareWith']])) {
return 1;
}
return $lastComments[$a['value']['shareWith']] - $lastComments[$b['value']['shareWith']];
return $lastComments[$b['value']['shareWith']]->getTimestamp() - $lastComments[$a['value']['shareWith']]->getTimestamp();
});
}
}

96
tests/php/Chat/AutoComplete/SorterTest.php

@ -0,0 +1,96 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Spreed\Tests\php\Chat;
use OCA\Spreed\Chat\AutoComplete\Sorter;
use OCA\Spreed\Chat\CommentsManager;
use PHPUnit\Framework\MockObject\MockObject;
class SorterTest extends \Test\TestCase {
/** @var CommentsManager|MockObject */
protected $commentsManager;
/** @var string */
protected $userId;
/** @var Sorter */
protected $sorter;
protected $user1 = [
'label' => 'Seattle',
'value' => [
'shareType' => 'user',
'shareWith' => 'seattle',
],
];
protected $user2 = [
'label' => 'New York',
'value' => [
'shareType' => 'user',
'shareWith' => 'new_york',
],
];
public function setUp() {
parent::setUp();
$this->commentsManager = $this->createMock(CommentsManager::class);
$this->sorter = new Sorter($this->commentsManager);
}
public function testGetId() {
$this->assertSame('talk_chat_participants', $this->sorter->getId());
}
public function dataSort(): array {
return [
'no user posted' => [['users' => [$this->user1, $this->user2]], [], ['users' => [$this->user1, $this->user2]]],
'second user posted' => [['users' => [$this->user1, $this->user2]], ['new_york' => new \DateTime('2000-01-01')], ['users' => [$this->user2, $this->user1]]],
'second user posted later' => [['users' => [$this->user1, $this->user2]], ['seattle' => new \DateTime('2017-01-01'), 'new_york' => new \DateTime('2018-01-01')], ['users' => [$this->user2, $this->user1]]],
'second user posted earlier' => [['users' => [$this->user1, $this->user2]], ['seattle' => new \DateTime('2018-01-01'), 'new_york' => new \DateTime('2017-01-01')], ['users' => [$this->user1, $this->user2]]],
'no users' => [['groups' => [$this->user1, $this->user2]], null, ['groups' => [$this->user1, $this->user2]]],
];
}
/**
* @dataProvider dataSort
*
* @param array $toSort
* @param array $comments
* @param array $expected
*/
public function testSort($toSort, $comments, $expected) {
$this->commentsManager->expects(isset($toSort['users']) ? $this->once() : $this->never())
->method('getLastCommentDateByActor')
->with('chat', 23, 'comment', 'users', $this->anything())
->willReturn($comments);
$this->sorter->sort($toSort, [
'itemType' => 'chat',
'itemId' => 23,
]);
$this->assertSame($expected, $toSort);
}
}
Loading…
Cancel
Save