Skip to content

Commit

Permalink
fix(core): prevent issues with unsaved data and system log
Browse files Browse the repository at this point in the history
  • Loading branch information
jeabakker committed Nov 4, 2022
1 parent 8967c46 commit 4048eee
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion engine/classes/ElggAccessCollection.php
Expand Up @@ -234,7 +234,7 @@ public function toObject(array $params = []) {
* {@inheritdoc}
*/
public function getSystemLogID(): int {
return $this->id;
return (int) $this->id;
}

/**
Expand Down
7 changes: 2 additions & 5 deletions engine/classes/ElggEntity.php
Expand Up @@ -1746,13 +1746,10 @@ public function getLongitude(): float {
*/

/**
* Return an identification for the object for storage in the system log.
* This id must be an integer.
*
* @return int
* {@inheritdoc}
*/
public function getSystemLogID(): int {
return $this->getGUID();
return (int) $this->getGUID();
}

/**
Expand Down
7 changes: 2 additions & 5 deletions engine/classes/ElggExtender.php
Expand Up @@ -180,13 +180,10 @@ public function toObject(array $params = []) {
*/

/**
* Return an identification for the object for storage in the system log.
* This id must be an integer.
*
* @return int
* {@inheritdoc}
*/
public function getSystemLogID(): int {
return $this->id;
return (int) $this->id;
}

/**
Expand Down
7 changes: 2 additions & 5 deletions engine/classes/ElggRelationship.php
Expand Up @@ -197,13 +197,10 @@ public function toObject(array $params = []) {
// SYSTEM LOG INTERFACE ////////////////////////////////////////////////////////////

/**
* Return an identification for the object for storage in the system log.
* This id must be an integer.
*
* @return int
* {@inheritdoc}
*/
public function getSystemLogID(): int {
return $this->id;
return (int) $this->id;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion engine/classes/Loggable.php
Expand Up @@ -12,7 +12,7 @@
interface Loggable {
/**
* Return an identification for the object for storage in the system log.
* This id must be an integer.
* This id must be an integer. Unsaved implementations should return 0.
*
* @return int
*/
Expand Down
5 changes: 5 additions & 0 deletions engine/tests/phpunit/unit/ElggAccessCollectionUnitTest.php
Expand Up @@ -21,4 +21,9 @@ public function invalidValuesProvider() {
['owner_guid', -1],
];
}

public function testIsLoggable() {
$unsaved = new \ElggAccessCollection();
$this->assertEmpty($unsaved->getSystemLogID());
}
}
3 changes: 3 additions & 0 deletions engine/tests/phpunit/unit/ElggGroupUnitTest.php
Expand Up @@ -47,6 +47,9 @@ public function testCanArrayAccessAttributes() {
}

public function testIsLoggable() {
$unsaved = new \ElggGroup();
$this->assertEmpty($unsaved->getSystemLogID());

$group = $this->createGroup();

$this->assertEquals($group->guid, $group->getSystemLogID());
Expand Down
3 changes: 3 additions & 0 deletions engine/tests/phpunit/unit/ElggMetadataUnitTest.php
Expand Up @@ -209,6 +209,9 @@ public function testCanArrayAccessAttributes() {
}

public function testIsLoggable() {
$unsaved = new \ElggMetadata();
$this->assertEmpty($unsaved->getSystemLogID());

$object = $this->createObject();
$metadata = new ElggMetadata();
$metadata->entity_guid = $object->guid;
Expand Down
3 changes: 3 additions & 0 deletions engine/tests/phpunit/unit/ElggObjectUnitTest.php
Expand Up @@ -232,6 +232,9 @@ public function testCanArrayAccessAttributes() {
}

public function testIsLoggable() {
$unsaved = new \ElggObject();
$this->assertEmpty($unsaved->getSystemLogID());

$object = $this->createObject();

$this->assertEquals($object->guid, $object->getSystemLogID());
Expand Down
3 changes: 3 additions & 0 deletions engine/tests/phpunit/unit/ElggRelationshipUnitTest.php
Expand Up @@ -93,6 +93,9 @@ public function testCanArrayAccessAttributes() {
}

public function testIsLoggable() {
$unsaved = new \ElggRelationship(new \stdClass());
$this->assertEmpty($unsaved->getSystemLogID());

$relationship = $this->createRelationship();

$this->assertEquals($relationship->id, $relationship->getSystemLogID());
Expand Down
3 changes: 3 additions & 0 deletions engine/tests/phpunit/unit/ElggSiteUnitTest.php
Expand Up @@ -93,6 +93,9 @@ public function testCanArrayAccessAttributes() {
}

public function testIsLoggable() {
$unsaved = new \ElggSite();
$this->assertEmpty($unsaved->getSystemLogID());

$site = elgg_get_site_entity();

$this->assertEquals($site->guid, $site->getSystemLogID());
Expand Down
3 changes: 3 additions & 0 deletions engine/tests/phpunit/unit/ElggUserUnitTest.php
Expand Up @@ -96,6 +96,9 @@ public function testCanArrayAccessAttributes() {
}

public function testIsLoggable() {
$unsaved = new \ElggUser();
$this->assertEmpty($unsaved->getSystemLogID());

$user = $this->createUser();

$this->assertEquals($user->guid, $user->getSystemLogID());
Expand Down
4 changes: 2 additions & 2 deletions mod/system_log/classes/Elgg/SystemLog/SystemLog.php
Expand Up @@ -110,7 +110,7 @@ public function rowToSystemLogEntry(\stdClass $row): SystemLogEntry {
protected function prepareObjectForInsert(\Loggable $object): \stdClass {
$insert = new \stdClass();

$insert->object_id = (int) $object->getSystemLogID();
$insert->object_id = $object->getSystemLogID();
$insert->object_class = get_class($object);
$insert->object_type = $object->getType();
$insert->object_subtype = $object->getSubtype();
Expand Down Expand Up @@ -152,7 +152,7 @@ public function insert($object, $event): void {
return;
}

if (!$object instanceof \Loggable) {
if (!$object instanceof \Loggable || empty($object->getSystemLogID())) {
return;
}

Expand Down

0 comments on commit 4048eee

Please sign in to comment.