From 19d2c39d0d6aba1e35e266b4e955670539364f81 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Mon, 15 Jun 2020 13:24:28 +0100 Subject: [PATCH] Issue #3150474 by jungle, munish.kumar: Inaccurate return type of \Drupal\views\Views::getView() --- core/modules/views/src/Views.php | 5 +++-- core/modules/views/tests/src/Unit/ViewsTest.php | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/core/modules/views/src/Views.php b/core/modules/views/src/Views.php index 86dcdf7a9a01..42ae43118a88 100644 --- a/core/modules/views/src/Views.php +++ b/core/modules/views/src/Views.php @@ -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; } /** diff --git a/core/modules/views/tests/src/Unit/ViewsTest.php b/core/modules/views/tests/src/Unit/ViewsTest.php index f2db710a39ae..813c88d980a4 100644 --- a/core/modules/views/tests/src/Unit/ViewsTest.php +++ b/core/modules/views/tests/src/Unit/ViewsTest.php @@ -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; @@ -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 * -- GitLab