Skip to content

Commit

Permalink
Issue #3150474 by jungle, munish.kumar: Inaccurate return type of \Dr…
Browse files Browse the repository at this point in the history
…upal\views\Views::getView()

(cherry picked from commit 19d2c39d0d6aba1e35e266b4e955670539364f81)
  • Loading branch information
alexpott committed Jun 15, 2020
1 parent eb19d42 commit 6506886
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions modules/views/src/Views.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,15 @@ public static function handlerManager($type) {
* @param string $id
* The view ID to load.
*
* @return \Drupal\views\ViewExecutable
* A view executable instance, from the loaded entity.
* @return \Drupal\views\ViewExecutable|null
* A view executable instance or NULL if the view does not exist.
*/
public static function getView($id) {
$view = \Drupal::entityTypeManager()->getStorage('view')->load($id);
if ($view) {
return static::executableFactory()->get($view);
}
return NULL;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions modules/views/tests/src/Unit/ViewsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\Tests\views\Unit;

use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\views\Views;
Expand Down Expand Up @@ -72,6 +73,21 @@ public function testGetView() {
$this->assertEquals(spl_object_hash($view), spl_object_hash($executable->storage));
}

/**
* Tests the getView() method against a non-existent view.
*
* @covers ::getView
*/
public function testGetNonExistentView() {
$entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
$storage = $this->prophesize(EntityStorageInterface::class);
$storage->load('test_view_non_existent')->willReturn(NULL);
$entity_type_manager->getStorage('view')->willReturn($storage->reveal());
$this->container->set('entity_type.manager', $entity_type_manager->reveal());
$executable_does_not_exist = Views::getView('test_view_non_existent');
$this->assertNull($executable_does_not_exist);
}

/**
* @covers ::getApplicableViews
*
Expand Down

0 comments on commit 6506886

Please sign in to comment.