Merge pull request #4380 from humhub/enh/ContentIsEdited

Enh: Implementation of Content::isEdited()
This commit is contained in:
buddh4 2020-09-16 15:35:37 +02:00 committed by GitHub
commit 4f5b59d25b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 143 additions and 0 deletions

View File

@ -291,4 +291,14 @@ class Comment extends ContentAddonActiveRecord implements ContentOwner
return false;
}
/**
* TODO: Unify with Content::isUpdated() see https://github.com/humhub/humhub/pull/4380
* @returns boolean true if this comment has been updated, otherwise false
* @since 1.7
*/
public function isUpdated()
{
return $this->created_at !== $this->updated_at && !empty($this->updated_at) && is_string($this->updated_at);
}
}

View File

@ -0,0 +1,63 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*
*/
namespace tests\codeception\unit\modules\content;
use humhub\modules\comment\models\Comment;
use tests\codeception\_support\HumHubDbTestCase;
use humhub\modules\post\models\Post;
class CommentEditTest extends HumHubDbTestCase
{
public function testNewCommentIsNotEdited()
{
$this->becomeUser('User2');
$comment = new Comment([
'message' => 'User2 comment!',
'object_model' => Post::class,
'object_id' => 11
]);
$this->assertTrue($comment->save());
$this->assertFalse($comment->isUpdated());
// Reload content
$comment = Comment::findOne(['id' => $comment->id]);
$this->assertFalse($comment->content->isUpdated());
}
public function testEditedContentIsEdited()
{
$this->becomeUser('User2');
$comment = new Comment([
'message' => 'User2 comment!',
'object_model' => Post::class,
'object_id' => 11
]);
$this->assertTrue($comment->save());
// Wait a second in order to prevent created_at = edited_at
sleep(1);
// Reload content
$comment = Comment::findOne(['id' => $comment->id]);
$comment->message = 'Updated Message';
$this->assertTrue($comment->save());
// See https://github.com/humhub/humhub/issues/4381
$comment->refresh();
$this->assertTrue($comment->isUpdated());
// Reload content
$comment = Comment::findOne(['id' => $comment->id]);
$this->assertTrue($comment->isUpdated());
}
}

View File

@ -538,6 +538,7 @@ class Content extends ActiveRecord implements Movable, ContentOwner
if (!$container) {
$container = $this->container;
}
return $this->getModel()->isOwner() || Yii::$app->user->can(ManageUsers::class) || $container->can(ManageContent::class);
}
@ -877,4 +878,13 @@ class Content extends ActiveRecord implements Movable, ContentOwner
{
return $this->getModel()->getContentDescription();
}
/**
* @returns boolean true if this content has been updated, otherwise false
* @since 1.7
*/
public function isUpdated()
{
return $this->created_at !== $this->updated_at && !empty($this->updated_at) && is_string($this->updated_at);
}
}

View File

@ -0,0 +1,60 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*
*/
namespace tests\codeception\unit\modules\content;
use tests\codeception\_support\HumHubDbTestCase;
use humhub\modules\post\models\Post;
use humhub\modules\space\models\Space;
use humhub\modules\content\models\Content;
class ContentEditTest extends HumHubDbTestCase
{
public function testNewContentIsNotEdited()
{
$this->becomeUser('User2');
$space = Space::findOne(['id' => 2]);
$post1 = new Post($space, Content::VISIBILITY_PUBLIC, ['message' => 'Test']);
$this->assertTrue($post1->save());
$this->assertFalse($post1->content->isEdited());
// Reload content
$post1 = Post::findOne(['id' => $post1->id]);
$this->assertFalse($post1->content->isEdited());
}
public function testEditedContentIsEdited()
{
$this->becomeUser('User2');
$space = Space::findOne(['id' => 2]);
$post1 = new Post($space, Content::VISIBILITY_PUBLIC, ['message' => 'Test']);
$this->assertTrue($post1->save());
// Wait a second in order to prevent created_at = edited_at
sleep(1);
// Reload content
$post1 = Post::findOne(['id' => $post1->id]);
$post1->message = 'Updated Message';
$this->assertTrue($post1->save());
// See https://github.com/humhub/humhub/issues/4381
$post1->refresh();
$this->assertTrue($post1->content->isEdited());
// Reload content
$post1 = Post::findOne(['id' => $post1->id]);
$this->assertTrue($post1->content->isEdited());
}
}