Skip to content

Commit

Permalink
fix(annotations): save/update annotations return false on null values
Browse files Browse the repository at this point in the history
fixes #14014
  • Loading branch information
jdalsem committed Jun 22, 2022
1 parent 77ff14c commit 4947ede
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
8 changes: 8 additions & 0 deletions engine/classes/Elgg/Database/AnnotationsTable.php
Expand Up @@ -102,6 +102,10 @@ public function create(\ElggAnnotation $annotation, \ElggEntity $entity) {
if ($annotation->id) {
return $this->update($annotation);
}

if (is_null($annotation->owner_guid) || is_null($annotation->name) || is_null($annotation->value)) {
return false;
}

$annotation->entity_guid = $entity->guid;

Expand Down Expand Up @@ -165,6 +169,10 @@ public function update(\ElggAnnotation $annotation) {
if (!$annotation->canEdit()) {
return false;
}

if (is_null($annotation->owner_guid) || is_null($annotation->name) || is_null($annotation->value)) {
return false;
}

if (!$this->events->triggerBefore('update', 'annotation', $annotation)) {
return false;
Expand Down
@@ -0,0 +1,112 @@
<?php

namespace Elgg\Database;

use Elgg\IntegrationTestCase;

class AnnotationsTableIntegrationTest extends IntegrationTestCase {

/**
* @var AnnotationsTable
*/
protected $service;

/**
* @var \ElggUser
*/
protected $owner;

/**
* {@inheritDoc}
*/
public function up() {
$this->service = _elgg_services()->annotationsTable;
$this->owner = $this->createUser();
_elgg_services()->session->setLoggedInUser($this->owner);
}

public function testCreateAnnotation() {
$entity = $this->createObject();

$annotation = new \ElggAnnotation();
$annotation->owner_guid = $this->owner->guid;
$annotation->name = 'foo';
$annotation->value = 'bar';

$annotation_id = $this->service->create($annotation, $entity);
$this->assertIsInt($annotation_id);

$annotation = elgg_get_annotation_from_id($annotation_id);
$this->assertInstanceOf(\ElggAnnotation::class, $annotation);
$this->assertEquals($this->owner->guid, $annotation->owner_guid);
$this->assertEquals('foo', $annotation->name);
$this->assertEquals('bar', $annotation->value);
}

public function testCreateAnnotationWithEmptyContent() {
$entity = $this->createObject();

$annotation = new \ElggAnnotation();

$this->assertFalse($this->service->create($annotation, $entity));
}

public function testUpdateAnnotationUsingUpdate() {
$entity = $this->createObject();

$annotation = new \ElggAnnotation();
$annotation->owner_guid = $this->owner->guid;
$annotation->name = 'foo';
$annotation->value = 'bar';

$annotation_id = $this->service->create($annotation, $entity);
$this->assertIsInt($annotation_id);

$annotation = elgg_get_annotation_from_id($annotation_id);
$this->assertInstanceOf(\ElggAnnotation::class, $annotation);
$annotation->value = 'bar2';
$annotation_save_id = $this->service->update($annotation);
$this->assertEquals($annotation_id, $annotation_save_id);

$annotation = elgg_get_annotation_from_id($annotation_id);
$this->assertEquals('bar2', $annotation->value);
}

public function testUpdateAnnotationUsingCreate() {
$entity = $this->createObject();

$annotation = new \ElggAnnotation();
$annotation->owner_guid = $this->owner->guid;
$annotation->name = 'foo';
$annotation->value = 'bar';

$annotation_id = $this->service->create($annotation, $entity);
$this->assertIsInt($annotation_id);

$annotation = elgg_get_annotation_from_id($annotation_id);
$this->assertInstanceOf(\ElggAnnotation::class, $annotation);
$annotation->value = 'bar2';
$annotation_save_id = $this->service->create($annotation, $entity);
$this->assertEquals($annotation_id, $annotation_save_id);

$annotation = elgg_get_annotation_from_id($annotation_id);
$this->assertEquals('bar2', $annotation->value);
}

public function testUpdateAnnotationWithEmptyContent() {
$entity = $this->createObject();

$annotation = new \ElggAnnotation();
$annotation->owner_guid = $this->owner->guid;
$annotation->name = 'foo';
$annotation->value = 'bar';

$annotation_id = $this->service->create($annotation, $entity);
$this->assertIsInt($annotation_id);

$annotation = elgg_get_annotation_from_id($annotation_id);
$this->assertInstanceOf(\ElggAnnotation::class, $annotation);
unset($annotation->value);
$this->assertFalse($this->service->update($annotation));
}
}

0 comments on commit 4947ede

Please sign in to comment.