Permalink
Browse files

Finished publishing logic, now also works to publish/unpublish a content

  • Loading branch information...
onigoetz committed Jun 7, 2016
1 parent 5a44ae7 commit 0c75f6842ebd2b3f611df380682996f5c3bc8d9f
Showing with 137 additions and 17 deletions.
  1. +7 −0 src/Entities/Content.php
  2. +18 −5 src/Entities/Entity.php
  3. +7 −0 src/Entities/Revision.php
  4. +105 −12 tests/Entities/EntityManagerTest.php
View
@@ -21,6 +21,13 @@ class Content extends Model
'published' => 'boolean',
];
/**
* {@inheritdoc}
*/
protected $attributes = [
'published' => true,
];
/**
* Get the revisions for this class
*
View
@@ -21,7 +21,8 @@
* @property int $revision_id The current revision id
* @property string $type The type of the Entity
* @property bool $published Is this content published
* @property \Rocket\Entities\Revision[] $revisions all revisions in this content
* @property-read bool $publishedRevision Is the revision published
* @property-read \Rocket\Entities\Revision[] $revisions all revisions in this content
* @property-read \DateTime $created_at
* @property-read \DateTime $updated_at
*/
@@ -224,6 +225,10 @@ public function __get($key)
return $this->content->revisions;
}
if ($key == 'publishedRevision') {
return $this->revision->published;
}
throw new NonExistentFieldException("Field '$key' doesn't exist in '" . get_class($this) . "'");
}
@@ -310,9 +315,10 @@ protected function getFieldTypes()
* @throws RevisionNotFoundException
* @return Revision
*/
protected static function findRevision($id, $language_id, $revision_id = null) {
protected static function findRevision($id, $language_id, $revision_id = null)
{
try {
if ($revision_id) {
if (is_numeric($revision_id) && $revision_id != 0) {
$revision = Revision::findOrFail($revision_id);
if ($revision->content_id != $id) {
@@ -327,8 +333,7 @@ protected static function findRevision($id, $language_id, $revision_id = null) {
->where('published', true)
->firstOrFail();
} catch (ModelNotFoundException $e) {
if ($revision_id) {
if (is_numeric($revision_id) && $revision_id != 0) {
throw new RevisionNotFoundException("This revision doesn't exist", 0, $e);
}
@@ -569,4 +574,12 @@ protected function clearFields()
$field->syncOriginal();
}
}
public function publishRevision()
{
$this->revision->published = true;
$this->revision->save();
$this->unpublishOtherRevisions();
}
}
@@ -22,6 +22,13 @@ class Revision extends Model
'published' => 'boolean',
];
/**
* {@inheritdoc}
*/
protected $attributes = [
'published' => true,
];
/**
* Get the revisions for this class
*
@@ -46,8 +46,8 @@ public function testSaveEntity()
// no id's exist before save
$array = $demo->toArray();
$this->assertEquals(['type' => 'demo'], $array['_content']);
$this->assertEquals(['language_id' => $language_id], $array['_revision']);
$this->assertEquals(['type' => 'demo', 'published' => true], $array['_content']);
$this->assertEquals(['language_id' => $language_id, 'published' => true], $array['_revision']);
// WHEN this entity is saved
$demo->save();
@@ -254,22 +254,115 @@ public function testRetrieveOldRevisionAfterNewOneWasCreated()
$this->assertEquals($demoR1->toArray()['titles'], $demoR1Bis->toArray()['titles']);
}
public function testPublishOldRevision()
public function testEntityPublishedByDefault()
{
// TODO :: implement feature
$this->markTestSkipped('Not implemented yet');
$language_id = Language::value('id');
// GIVEN a base entity with two revisions
$demoR1 = new Demo($language_id);
$demoR1->titles[] = 'one title';
$demoR1->titles[] = 'two titles';
$demoR1->save();
$this->assertTrue($demoR1->published);
$this->assertTrue($demoR1->publishedRevision);
$this->assertTrue($demoR1->toArray()['_content']['published']);
$this->assertTrue($demoR1->toArray()['_revision']['published']);
$demoR2 = Demo::find($demoR1->id, $language_id);
$this->assertTrue($demoR2->published);
$this->assertTrue($demoR2->publishedRevision);
$this->assertTrue($demoR2->toArray()['_content']['published']);
$this->assertTrue($demoR2->toArray()['_revision']['published']);
}
public function testPublishOtherRevision()
public function testPublishOldRevision()
{
// TODO :: implement feature
$this->markTestSkipped('Not implemented yet');
$language_id = Language::value('id');
// GIVEN a base entity with two revisions
$demoR1 = new Demo($language_id);
$demoR1->titles[] = 'one title';
$demoR1->titles[] = 'two titles';
$demoR1->save();
$demoR2 = Demo::find($demoR1->id, $language_id);
$demoR2->titles[1] = 'new title';
unset($demoR2->titles[0]);
$demoR2->save(true);
// THEN the second revision is published automatically
$demoR2Bis = Demo::find($demoR1->id, $language_id);
$this->assertEquals($demoR2->revision_id, $demoR2Bis->revision_id);
// THEN the first revision is unpublished
$demoR1Bis = Demo::find($demoR1->id, $language_id, $demoR1->revision_id);
$this->assertFalse($demoR1Bis->publishedRevision);
// WHEN we republish the old revision
$demoR1Bis->publishRevision();
// THEN the first revision is published again
$this->assertTrue($demoR1Bis->publishedRevision);
$demoR1BisBis = Demo::find($demoR1->id, $language_id);
$this->assertEquals($demoR1->revision_id, $demoR1BisBis->revision_id);
// THEN the second revision is unpublished
$demoR2BisBis = Demo::find($demoR1->id, $language_id, $demoR2->revision_id);
$this->assertFalse($demoR2BisBis->publishedRevision);
}
public function testUnpublishContent()
{
// TODO :: implement feature
$this->markTestSkipped('Not implemented yet');
$language_id = Language::value('id');
// GIVEN a base entity
$demo = new Demo($language_id);
$demo->titles[] = 'one title';
$demo->titles[] = 'two titles';
$demo->save();
// THEN the content is published
$demoBis = Demo::find($demo->id, $language_id);
$this->assertTrue($demoBis->published);
$this->assertTrue($demoBis->publishedRevision);
// WHEN it is unpublished
$demoBis->published = false;
$demoBis->save();
// THEN we can retrieve it but it is unpublished
$demoBisBis = Demo::find($demo->id, $language_id);
$this->assertFalse($demoBisBis->published);
$this->assertTrue($demoBisBis->publishedRevision);
}
public function testDeleteRevisionWillRepublishOldRevision()
{
$language_id = Language::value('id');
// GIVEN a base entity with two revisions
$demoR1 = new Demo($language_id);
$demoR1->titles[] = 'one title';
$demoR1->titles[] = 'two titles';
$demoR1->save();
$demoR2 = Demo::find($demoR1->id, $language_id);
$demoR2->titles[1] = 'new title';
unset($demoR2->titles[0]);
$demoR2->save(true);
// THEN on load, the second revision is taken
$demoR2Bis = Demo::find($demoR1->id, $language_id);
$this->assertEquals($demoR2->revision_id, $demoR2Bis->revision_id);
// WHEN we delete the newest revision
$demoR2->deleteRevision();
// WHEN we get this entity
$demoR1Bis = Demo::find($demoR1->id, $language_id);
$this->assertEquals($demoR1->revision_id, $demoR1Bis->revision_id);
}
public function testDeleteRevisionCleared()
@@ -280,7 +373,7 @@ public function testDeleteRevisionCleared()
$demo = new Demo($language_id);
$demo->titles[] = 'one title';
$demo->titles[] = 'two titles';
$demo->save(true, false);
$demo->save();
$demo->deleteRevision();
@@ -306,7 +399,7 @@ public function testDeleteRevisionUnCleared()
$demo = new Demo($language_id);
$demo->titles[] = 'one title';
$demo->titles[] = 'two titles';
$demo->save(true, false);
$demo->save();
$demo->deleteRevision(false);

0 comments on commit 0c75f68

Please sign in to comment.