Commit 4d3e9456 authored by Guy Thouret's avatar Guy Thouret

(chore) Add missing spec tests for new classes; Refactor usage to not use...

(chore) Add missing spec tests for new classes; Refactor usage to not use model passed by reference as this is not allowed by phpspec (by design) - #616
1 merge request!303WIP: Propogate Properties when entities get updated
Pipeline #79224108 failed with stages
in 4 minutes and 7 seconds
......@@ -9,7 +9,7 @@ class PropagateProperties extends Properties
{
protected $actsOnSubtype = 'blog';
public function toActivity($from, Activity &$to): void
public function toActivity($from, Activity $to): Activity
{
if ($this->valueHasChanged($from->getTitle(), $to->get('title'))) {
$to->set('title', $from->getTitle());
......@@ -20,17 +20,19 @@ class PropagateProperties extends Properties
$to->set('blurb', $blurb);
}
if ($this->valueHasChanged($from->getURL(), $to->getURL())) {
$to->setURL($from->getURL());
if ($this->valueHasChanged($from->getUrl(), $to->getURL())) {
$to->setURL($from->getUrl());
}
if ($this->valueHasChanged($from->getIconUrl(), $to->get('thumbnail_src'))) {
$to->set('thumbnail_src', $from->getIconUrl());
}
return $to;
}
public function fromActivity(Activity $from, &$to): void
public function fromActivity(Activity $from, $to)
{
// TODO: Implement fromActivity() method.
return $to;
}
}
......@@ -7,7 +7,7 @@ use Minds\Entities\Activity;
class PropagateProperties extends Properties
{
public function toActivity($from, Activity &$to): void
public function toActivity($from, Activity $to): Activity
{
if ($this->valueHasChanged($from->getNsfw(), $to->getNsfw())) {
$to->setNsfw($from->getNsfw());
......@@ -16,9 +16,11 @@ class PropagateProperties extends Properties
if ($this->valueHasChanged($from->getNsfwLock(), $to->getNsfwLock())) {
$to->setNsfwLock($from->getNsfwLock());
}
return $to;
}
public function fromActivity(Activity $from, &$to): void
public function fromActivity(Activity $from, $to)
{
if ($this->valueHasChanged($from->getNsfw(), $to->getNsfw())) {
$to->setNsfw($from->getNsfw());
......@@ -27,5 +29,7 @@ class PropagateProperties extends Properties
if ($this->valueHasChanged($from->getNsfwLock(), $to->getNsfwLock())) {
$to->setNsfwLock($from->getNsfwLock());
}
return $to;
}
}
......@@ -41,6 +41,11 @@ class PropagateProperties
$this->addPropagator(Core\Permissions\Delegates\PropagateProperties::class);
}
public function clearPropogators()
{
$this->propagators = [];
}
protected function addPropagator(string $class): void
{
$obj = new $class();
......
......@@ -41,7 +41,17 @@ abstract class Properties
return $this->changed;
}
abstract public function toActivity($from, Activity &$to): void;
abstract public function fromActivity(Activity $from, &$to): void;
/**
* @param $from
* @param Activity $to
* @return Activity
*/
abstract public function toActivity($from, Activity $to): Activity;
/**
* @param Activity $from
* @param $to
* @return mixed
*/
abstract public function fromActivity(Activity $from, $to);
}
......@@ -7,25 +7,29 @@ use Minds\Entities\Activity;
class PropagateProperties extends Properties
{
public function toActivity($from, Activity &$to): void
public function toActivity($from, Activity $to): Activity
{
if ($this->valueHasChanged($from->getModeratorGuid(), $to->getModeratorGuid())) {
$to->setModeratorGuid($from->getModeratorGuid());
}
if ($this->valueHasChanged($from->getTimeModerated(), $to->getModeratorGuid())) {
if ($this->valueHasChanged($from->getTimeModerated(), $to->getTimeModerated())) {
$to->setTimeModerated($from->getTimeModerated());
}
return $to;
}
public function fromActivity(Activity $from, &$to): void
public function fromActivity(Activity $from, $to)
{
if ($this->valueHasChanged($from->getModeratorGuid(), $to->getModeratorGuid())) {
$to->setModeratorGuid($from->getModeratorGuid());
}
if ($this->valueHasChanged($from->getTimeModerated(), $to->getModeratorGuid())) {
if ($this->valueHasChanged($from->getTimeModerated(), $to->getTimeModerated())) {
$to->setTimeModerated($from->getTimeModerated());
}
return $to;
}
}
......@@ -10,7 +10,7 @@ class PropagateProperties extends Properties
protected $actsOnType = 'object';
protected $actsOnSubtype = ['image', 'video'];
public function toActivity($from, Activity &$to): void
public function toActivity($from, Activity $to): Activity
{
if ($this->valueHasChanged($from->title, $to->getMessage())) {
$to->setMessage($from->title);
......@@ -18,13 +18,15 @@ class PropagateProperties extends Properties
$fromData = $from->getActivityParameters();
$toData = $to->getCustom();
if ($this->valueHasChanged($fromData[1], $toData[1])) {
if ((!isset($toData[1])) || (isset($toData[1]) && $this->valueHasChanged($fromData[1], $toData[1]))) {
$to->setCustom($fromData[0], $fromData[1]);
}
return $to;
}
public function fromActivity(Activity $from, &$to): void
public function fromActivity(Activity $from, $to)
{
// TODO: Implement fromActivity() method.
return $to;
}
}
......@@ -7,17 +7,21 @@ use Minds\Entities\Activity;
class PropagateProperties extends Properties
{
public function toActivity($from, Activity &$to): void
public function toActivity($from, Activity $to): Activity
{
if ($this->valueHasChanged($from->getAllowComments(), $to->getAllowComments())) {
$to->setAllowComments($from->getAllowComments());
}
return $to;
}
public function fromActivity(Activity $from, &$to): void
public function fromActivity(Activity $from, $to)
{
if ($this->valueHasChanged($from->getAllowComments(), $to->getAllowComments())) {
$to->setAllowComments($from->getAllowComments());
}
return $to;
}
}
<?php
namespace Spec\Minds\Core\Blogs\Delegates;
use Minds\Core\Blogs\Blog;
use Minds\Entities\Activity;
use Minds\Entities\Entity;
use PhpSpec\ObjectBehavior;
class PropagatePropertiesSpec extends ObjectBehavior
{
/** @var Blog */
protected $blog;
/** @var Activity */
protected $activity;
public function let(
Blog $blog,
Activity $activity
)
{
$this->blog = $blog;
$this->activity = $activity;
}
public function it_is_initializable()
{
$this->shouldHaveType('Minds\Core\Blogs\Delegates\PropagateProperties');
}
public function it_should_propagate_changes_to_activity()
{
$this->blog->getTitle()->shouldBeCalled()->willReturn('New Title');
$this->activity->get('title')->shouldBeCalled()->willReturn('Old Title');
$this->activity->set('title', 'New Title')->shouldBeCalled();
$this->blog->getBody()->shouldBeCalled()->willReturn('body');
$this->activity->get('blurb')->shouldBeCalled()->willReturn('body');
$this->blog->getUrl()->shouldBeCalled()->willReturn('some url');
$this->activity->getURL()->shouldBeCalled()->willReturn('some url');
$this->blog->getIconUrl()->shouldBeCalled()->willReturn('some icon url');
$this->activity->get('thumbnail_src')->shouldBeCalled()->willReturn('some other url');
$this->activity->set('thumbnail_src', 'some icon url')->shouldBeCalled();
$this->toActivity($this->blog, $this->activity);
}
}
<?php
namespace Spec\Minds\Core\Entities\Delegates;
use Minds\Core\Blogs\Blog;
use Minds\Entities\Activity;
use PhpSpec\ObjectBehavior;
class PropagatePropertiesSpec extends ObjectBehavior
{
/** @var Blog */
protected $blog;
/** @var Activity */
protected $activity;
public function let(
Blog $blog,
Activity $activity
)
{
$this->blog = $blog;
$this->activity = $activity;
}
public function it_is_initializable()
{
$this->shouldHaveType('Minds\Core\Entities\Delegates\PropagateProperties');
}
public function it_should_propagate_changes_to_activity()
{
$this->blog->getNsfw()->shouldBeCalled()->willReturn([1]);
$this->activity->getNsfw()->shouldBeCalled()->willReturn([]);
$this->activity->setNsfw([1])->shouldBeCalled();
$this->blog->getNsfwLock()->shouldBeCalled()->willReturn([1]);
$this->activity->getNsfwLock()->shouldBeCalled()->willReturn([]);
$this->activity->setNsfwLock([1])->shouldBeCalled();
$this->toActivity($this->blog, $this->activity);
}
public function it_should_propogate_properties_from_activity()
{
$this->activity->getNsfw()->shouldBeCalled()->willReturn([1]);
$this->blog->getNsfw()->shouldBeCalled()->willReturn([]);
$this->blog->setNsfw([1])->shouldBeCalled();
$this->activity->getNsfwLock()->shouldBeCalled()->willReturn([1]);
$this->blog->getNsfwLock()->shouldBeCalled()->willReturn([]);
$this->blog->setNsfwLock([1])->shouldBeCalled();
$this->fromActivity($this->activity, $this->blog);
}
}
<?php
namespace Spec\Minds\Core\Entities;
use Minds\Core\Data\Call;
use Minds\Core\Entities\Actions\Save;
use Minds\Core\EntitiesBuilder;
use Minds\Entities\Activity;
use Minds\Entities\Entity;
use PhpSpec\ObjectBehavior;
class PropagatePropertiesSpec extends ObjectBehavior
{
protected $db;
protected $save;
protected $entitiesBuilder;
protected $propagator;
protected $activity;
protected $entity;
public function let(
Call $db,
Save $save,
EntitiesBuilder $entitiesBuilder,
Activity $activity,
Entity $entity
)
{
$this->beConstructedWith($db, $save, $entitiesBuilder);
$this->db = $db;
$this->save = $save;
$this->entitiesBuilder = $entitiesBuilder;
$this->activity = $activity;
$this->entity = $entity;
$this->clearPropogators();
}
public function it_is_initializable()
{
$this->shouldHaveType('Minds\Core\Entities\PropagateProperties');
}
public function it_should_call_from_activity()
{
$this->activity->get('entity_guid')->shouldBeCalled()->willReturn(1001);
$this->entitiesBuilder->single(1001)->shouldBeCalled()->willReturn($this->entity);
$this->from($this->activity);
}
public function it_should_call_to_activities()
{
$this->entity->getGUID()->shouldBeCalled()->willReturn(1002);
$this->db->getRow("activity:entitylink:1002")->shouldBeCalled()->willReturn([1001 => 12345]);
$this->entitiesBuilder->single(1001)->shouldBeCalled()->willReturn($this->activity);
$this->from($this->entity);
}
}
<?php
namespace Spec\Minds\Core\Feeds\Delegates;
use Minds\Entities\Activity;
use Minds\Entities\Entity;
use PhpSpec\ObjectBehavior;
class PropagatePropertiesSpec extends ObjectBehavior
{
/** @var Entity */
protected $entity;
/** @var Activity */
protected $activity;
public function let(
Entity $entity,
Activity $activity
)
{
$this->entity = $entity;
$this->activity = $activity;
}
public function it_is_initializable()
{
$this->shouldHaveType('Minds\Core\Feeds\Delegates\PropagateProperties');
}
public function it_should_propagate_changes_to_activity()
{
$this->entity->getModeratorGuid()->shouldBeCalled()->willReturn('12345');
$this->activity->getModeratorGuid()->shouldBeCalled()->willReturn('6789');
$this->activity->setModeratorGuid('12345')->shouldBeCalled();
$this->entity->getTimeModerated()->shouldBeCalled()->willReturn(12345);
$this->activity->getTimeModerated()->shouldBeCalled()->willReturn(6789);
$this->activity->setTimeModerated(12345)->shouldBeCalled();
$this->toActivity($this->entity, $this->activity);
}
public function it_should_propogate_properties_from_activity()
{
$this->activity->getModeratorGuid()->shouldBeCalled()->willReturn('12345');
$this->entity->getModeratorGuid()->shouldBeCalled()->willReturn('6789');
$this->entity->setModeratorGuid('12345')->shouldBeCalled();
$this->activity->getTimeModerated()->shouldBeCalled()->willReturn(12345);
$this->entity->getTimeModerated()->shouldBeCalled()->willReturn(6789);
$this->entity->setTimeModerated(12345)->shouldBeCalled();
$this->fromActivity($this->activity, $this->entity);
}
}
<?php
namespace Spec\Minds\Core\Media\Delegates;
use Minds\Entities\Activity;
use Minds\Entities\Image;
use PhpSpec\ObjectBehavior;
class PropagatePropertiesSpec extends ObjectBehavior
{
/** @var Image */
protected $entity;
/** @var Activity */
protected $activity;
public function let(
Image $entity,
Activity $activity
)
{
$this->entity = $entity;
$this->activity = $activity;
}
public function it_is_initializable()
{
$this->shouldHaveType('Minds\Core\Media\Delegates\PropagateProperties');
}
public function it_should_propagate_changes_to_activity()
{
$this->entity->get('title')->shouldBeCalled()->willReturn('new title');
$this->activity->getMessage()->shouldBeCalled()->willReturn('old title');
$this->activity->setMessage('new title')->shouldBeCalled();
$activityParameters = [
'batch',
[
'key1' => 'value1',
'key2' => 'value2'
]
];
$this->entity->getActivityParameters()->shouldBeCalled()->willReturn($activityParameters);
$this->activity->getCustom()->shouldBeCalled()->willReturn([]);
$this->activity->setCustom($activityParameters[0], $activityParameters[1])->shouldBeCalled();
$this->toActivity($this->entity, $this->activity);
}
public function it_should_propogate_properties_from_activity()
{
$this->fromActivity($this->activity, $this->entity);
}
}
<?php
namespace Spec\Minds\Core\Permissions\Delegates;
use Minds\Entities\Activity;
use Minds\Entities\Entity;
use PhpSpec\ObjectBehavior;
class PropagatePropertiesSpec extends ObjectBehavior
{
/** @var Entity */
protected $entity;
/** @var Activity */
protected $activity;
public function let(
Entity $entity,
Activity $activity
)
{
$this->entity = $entity;
$this->activity = $activity;
}
public function it_is_initializable()
{
$this->shouldHaveType('Minds\Core\Permissions\Delegates\PropagateProperties');
}
public function it_should_propagate_changes_to_activity()
{
$this->entity->getAllowComments()->shouldBeCalled()->willReturn(true);
$this->activity->getAllowComments()->shouldBeCalled()->willReturn(false);
$this->activity->setAllowComments(true)->shouldBeCalled();
$this->toActivity($this->entity, $this->activity);
}
public function it_should_propogate_properties_from_activity()
{
$this->activity->getAllowComments()->shouldBeCalled()->willReturn(true);
$this->entity->getAllowComments()->shouldBeCalled()->willReturn(false);
$this->entity->setAllowComments(true)->shouldBeCalled();
$this->fromActivity($this->activity, $this->entity);
}
}
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