Unverified Commit fd046df3 authored by alexpott's avatar alexpott

Issue #3150474 by jungle, munish.kumar: Inaccurate return type of \Drupal\views\Views::getView()

(cherry picked from commit 19d2c39d)
parent 57735992
...@@ -115,14 +115,15 @@ public static function handlerManager($type) { ...@@ -115,14 +115,15 @@ public static function handlerManager($type) {
* @param string $id * @param string $id
* The view ID to load. * The view ID to load.
* *
* @return \Drupal\views\ViewExecutable * @return \Drupal\views\ViewExecutable|null
* A view executable instance, from the loaded entity. * A view executable instance or NULL if the view does not exist.
*/ */
public static function getView($id) { public static function getView($id) {
$view = \Drupal::entityTypeManager()->getStorage('view')->load($id); $view = \Drupal::entityTypeManager()->getStorage('view')->load($id);
if ($view) { if ($view) {
return static::executableFactory()->get($view); return static::executableFactory()->get($view);
} }
return NULL;
} }
/** /**
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\Tests\views\Unit; namespace Drupal\Tests\views\Unit;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
use Drupal\views\Views; use Drupal\views\Views;
...@@ -72,6 +73,21 @@ public function testGetView() { ...@@ -72,6 +73,21 @@ public function testGetView() {
$this->assertEquals(spl_object_hash($view), spl_object_hash($executable->storage)); $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 * @covers ::getApplicableViews
* *
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment