Verified Commit 3cf6940d authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3007424 by acbramley, Spokje, mbovan, narendra.rajwar27, AaronBauman,...

Issue #3007424 by acbramley, Spokje, mbovan, narendra.rajwar27, AaronBauman, ravi.shankar, aleevas, geek-merlin, jibran, YurkinPark, daffie, larowlan, Berdir, alexpott, marcelovani, catch, Lendude, mstrelan, tar_inet, Honza Pobořil, seanB, andrewbelcher, danflanagan8, susgo, sylus, sjpeters79, yogeshmpawar, raman.b: Multiple usages of FieldPluginBase::getEntity do not check for NULL, leading to WSOD
parent b20bccfd
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -76,7 +76,10 @@ public function render(ResultRow $values) {
    $entity = $this->getEntity($values);

    // Only render the links, if they are defined.
    return !empty($this->build[$entity->id()]['links']['comment__comment']) ? \Drupal::service('renderer')->render($this->build[$entity->id()]['links']['comment__comment']) : '';
    if (!$entity || empty($this->build[$entity->id()]['links']['comment__comment'])) {
      return '';
    }
    return \Drupal::service('renderer')->render($this->build[$entity->id()]['links']['comment__comment']);
  }

}
+5 −1
Original line number Diff line number Diff line
@@ -19,7 +19,11 @@ class LinkApprove extends LinkBase {
   * {@inheritdoc}
   */
  protected function getUrlInfo(ResultRow $row) {
    return Url::fromRoute('comment.approve', ['comment' => $this->getEntity($row)->id()]);
    $entity = $this->getEntity($row);
    if (!$entity) {
      return NULL;
    }
    return Url::fromRoute('comment.approve', ['comment' => $entity->id()]);
  }

  /**
+3 −0
Original line number Diff line number Diff line
@@ -21,6 +21,9 @@ class LinkReply extends LinkBase {
  protected function getUrlInfo(ResultRow $row) {
    /** @var \Drupal\comment\CommentInterface $comment */
    $comment = $this->getEntity($row);
    if (!$comment) {
      return NULL;
    }
    return Url::fromRoute('comment.reply', [
      'entity_type' => $comment->getCommentedEntityTypeId(),
      'entity' => $comment->getCommentedEntityId(),
+42 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\comment\Unit\Plugin\views\field;

use Drupal\comment\Plugin\views\field\EntityLink;
use Drupal\Tests\UnitTestCase;
use Drupal\Tests\views\Traits\ViewsLoggerTestTrait;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;

/**
 * @coversDefaultClass \Drupal\comment\Plugin\views\field\EntityLink
 * @group comment
 */
class EntityLinkTest extends UnitTestCase {

  use ViewsLoggerTestTrait;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    $this->setUpMockLoggerWithMissingEntity();
  }

  /**
   * Test the render method when getEntity returns NULL.
   *
   * @covers ::render
   */
  public function testRenderNullEntity(): void {
    $row = new ResultRow();
    $field = new EntityLink(['entity_type' => 'foo', 'entity field' => 'bar'], '', []);
    $view = $this->createMock(ViewExecutable::class);
    $display = $this->createMock(DisplayPluginBase::class);
    $field->init($view, $display);
    $this->assertEmpty($field->render($row));
  }

}
+50 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\comment\Unit\Plugin\views\field;

use Drupal\comment\Plugin\views\field\LinkApprove;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\Tests\views\Traits\ViewsLoggerTestTrait;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;

/**
 * @coversDefaultClass \Drupal\comment\Plugin\views\field\LinkApprove
 * @group comment
 */
class LinkApproveTest extends UnitTestCase {

  use ViewsLoggerTestTrait;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    $this->setUpMockLoggerWithMissingEntity();
    $container = \Drupal::getContainer();
    $container->set('string_translation', $this->createMock(TranslationInterface::class));
    \Drupal::setContainer($container);
  }

  /**
   * Test the render method when getEntity returns NULL.
   *
   * @covers ::render
   */
  public function testRenderNullEntity(): void {
    $row = new ResultRow();
    $field = new LinkApprove(['entity_type' => 'foo', 'entity field' => 'bar'], '', [], $this->createMock(AccessManagerInterface::class), $this->createMock(EntityTypeManagerInterface::class), $this->createMock(EntityRepositoryInterface::class), $this->createMock(LanguageManagerInterface::class));
    $view = $this->createMock(ViewExecutable::class);
    $display = $this->createMock(DisplayPluginBase::class);
    $field->init($view, $display);
    $this->assertEmpty($field->render($row));
  }

}
Loading