Skip to content

Commit

Permalink
Check if referenced node is already published before publishing again (
Browse files Browse the repository at this point in the history
  • Loading branch information
jastraat authored Feb 8, 2023
1 parent 1554061 commit c8a585b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
12 changes: 6 additions & 6 deletions modules/metastore/src/Reference/Referencer.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,12 +415,12 @@ private function checkExistingReference(string $property_id, $data) {
]);

if ($node = reset($nodes)) {
// If an existing but orphaned data node is found,
// change the state back to published.
// @todo if the referencing node is in a draft state, do not publish the
// referenced node.
$node->set('moderation_state', 'published');
$node->save();
// @todo if referencing node in draft state, don't publish referenced node
// If an existing referenced node is found but unpublished, publish it.
if ($node->get('moderation_state')->value !== "published") {
$node->set('moderation_state', 'published');
$node->save();
}
return $node->uuid();
}
return NULL;
Expand Down
46 changes: 24 additions & 22 deletions modules/metastore/tests/src/Unit/Reference/ReferencerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\File\FileSystem;
use Drupal\Core\Logger\LoggerChannelFactory;
use Drupal\Core\StreamWrapper\PublicStream;
Expand All @@ -19,6 +20,7 @@
use Drupal\metastore\Storage\DataFactory;
use Drupal\metastore\Storage\NodeData;
use Drupal\metastore\Storage\ResourceMapperDatabaseTable;
use Drupal\node\Entity\Node;
use Drupal\node\NodeStorage;

use GuzzleHttp\Exception\RequestException;
Expand Down Expand Up @@ -64,23 +66,23 @@ class ReferencerTest extends TestCase {

private function mockReferencer($existing = TRUE) {
if ($existing) {
$node = new class {
public function uuid() {
return '0398f054-d712-4e20-ad1e-a03193d6ab33';
}
public function set() {}
public function save() {}
};
$node = (new Chain($this))
->add(Node::class, 'get', FieldItemListInterface::class)
->addd('uuid', '0398f054-d712-4e20-ad1e-a03193d6ab33')
->add(FieldItemListInterface::class, 'getString', 'orphaned')
->add(Node::class, 'set')
->add(Node::class, 'save')
->getMock();
}
else {
$node = new class {
public function uuid() {
return NULL;
}
public function set() {}
public function save() {}
public function setRevisionLogMessage() {}
};
$node = (new Chain($this))
->add(Node::class, 'get', FieldItemListInterface::class)
->addd('uuid', null)
->add(FieldItemListInterface::class, 'getString', 'orphaned')
->add(Node::class, 'set')
->add(Node::class, 'save')
->add(Node::class, 'setRevisionLogMessage')
->getMock();
}

$storageFactory = (new Chain($this))
Expand Down Expand Up @@ -387,13 +389,13 @@ public function testHostify(): void {
*/
public function testMimeTypeDetection(): void {
// Initialize mock node class.
$node = new class {
public function uuid() {
return '0398f054-d712-4e20-ad1e-a03193d6ab33';
}
public function set() {}
public function save() {}
};
$node = (new Chain($this))
->add(Node::class, 'get', FieldItemListInterface::class)
->addd('uuid', '0398f054-d712-4e20-ad1e-a03193d6ab33')
->add(FieldItemListInterface::class, 'getString', 'orphaned')
->add(Node::class, 'set')
->add(Node::class, 'save')
->getMock();

// Create a mock file storage class.
$storage = new class {
Expand Down

0 comments on commit c8a585b

Please sign in to comment.