Issue #2978320 by quietone, Ivan616, daffie: rdf comment storage load should not load NULL comments

merge-requests/7/head
Lee Rowlands 2020-08-24 11:03:06 +10:00
parent 8fac18c866
commit 60a835cbe1
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
2 changed files with 53 additions and 6 deletions

View File

@ -237,12 +237,13 @@ function rdf_comment_storage_load($comments) {
->getPreparedFieldMapping('created');
/** @var \Drupal\comment\CommentInterface $comment*/
$comment->rdf_data['date'] = rdf_rdfa_attributes($created_mapping, $comment->get('created')->first()->toArray());
$entity = $comment->getCommentedEntity();
// The current function is a storage level hook, so avoid to bubble
// bubbleable metadata, because it can be outside of a render context.
$comment->rdf_data['entity_uri'] = $entity->toUrl()->toString(TRUE)->getGeneratedUrl();
if ($comment->hasParentComment()) {
$comment->rdf_data['pid_uri'] = $comment->getParentComment()->toUrl()->toString(TRUE)->getGeneratedUrl();
if ($entity = $comment->getCommentedEntity()) {
// The current function is a storage level hook, so avoid to bubble
// bubbleable metadata, because it can be outside of a render context.
$comment->rdf_data['entity_uri'] = $entity->toUrl()->toString(TRUE)->getGeneratedUrl();
}
if ($parent = $comment->getParentComment()) {
$comment->rdf_data['pid_uri'] = $parent->toUrl()->toString(TRUE)->getGeneratedUrl();
}
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace Drupal\Tests\rdf\Kernel;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\Field\Plugin\Field\FieldType\CreatedItem;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\comment\Entity\Comment;
/**
* Tests rdf_comment_storage_load.
*
* @group rdf
*/
class RdfCommentStorageLoadTest extends EntityKernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['comment', 'rdf'];
/**
* Tests rdf_comment_storage_load.
*/
public function testRdfCommentStorageLoad() {
$field_created_item = $this->prophesize(CreatedItem::class);
$field_created_item->setValue([time()]);
$field_list = $this->prophesize(FieldItemList::class);
$field_list->reveal();
$field_list->first()->willReturn($field_created_item->reveal());
$comment = $this->prophesize(Comment::class);
$comment->bundle()->willReturn('page');
$comment->get('created')->willReturn($field_list);
$comment->getFieldDefinitions()->willReturn(NULL);
// Set commented entity and parent entity to NULL.
$comment->getCommentedEntity()->willReturn(NULL);
$comment->getParentComment()->willReturn(NULL);
/** @var \Drupal\Core\Extension\ModuleHandler $module_handler */
$module_handler = \Drupal::service('module_handler');
$module_handler->invoke('rdf', 'comment_storage_load', [[$comment->reveal()]]);
}
}