Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views / tests / src / Unit / Controller / ViewAjaxControllerTest.php
index f766975e488b5049e9cc2fef3ceb966b9a518122..a02f098cda73f31f154689c5ced036ef0ea90100 100644 (file)
@@ -18,6 +18,9 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  */
 class ViewAjaxControllerTest extends UnitTestCase {
 
+  const USE_AJAX = TRUE;
+  const USE_NO_AJAX = FALSE;
+
   /**
    * The mocked view entity storage.
    *
@@ -186,23 +189,6 @@ class ViewAjaxControllerTest extends UnitTestCase {
 
     list($view, $executable) = $this->setupValidMocks();
 
-    $display_handler = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase')
-      ->disableOriginalConstructor()
-      ->getMock();
-    // Ensure that the pager element is not set.
-    $display_handler->expects($this->never())
-      ->method('setOption');
-
-    $display_collection = $this->getMockBuilder('Drupal\views\DisplayPluginCollection')
-      ->disableOriginalConstructor()
-      ->getMock();
-    $display_collection->expects($this->any())
-      ->method('get')
-      ->with('page_1')
-      ->will($this->returnValue($display_handler));
-
-    $executable->displayHandlers = $display_collection;
-
     $this->redirectDestination->expects($this->atLeastOnce())
       ->method('set')
       ->with('/test-page?type=article');
@@ -215,6 +201,24 @@ class ViewAjaxControllerTest extends UnitTestCase {
     $this->assertViewResultCommand($response);
   }
 
+  /**
+   * Tests a valid view without ajax enabled.
+   */
+  public function testAjaxViewWithoutAjax() {
+    $request = new Request();
+    $request->request->set('view_name', 'test_view');
+    $request->request->set('view_display_id', 'page_1');
+    $request->request->set('view_path', '/test-page');
+    $request->request->set('_wrapper_format', 'ajax');
+    $request->request->set('ajax_page_state', 'drupal.settings[]');
+    $request->request->set('type', 'article');
+
+    $this->setupValidMocks(static::USE_NO_AJAX);
+
+    $this->setExpectedException(AccessDeniedHttpException::class);
+    $this->viewAjaxController->ajaxView($request);
+  }
+
   /**
    * Tests a valid view with arguments.
    */
@@ -297,8 +301,15 @@ class ViewAjaxControllerTest extends UnitTestCase {
 
   /**
    * Sets up a bunch of valid mocks like the view entity and executable.
+   *
+   * @param bool $use_ajax
+   *   Whether the 'use_ajax' option is set on the view display. Defaults to
+   *   using ajax (TRUE).
+   *
+   * @return array
+   *   A pair of view storage entity and executable.
    */
-  protected function setupValidMocks() {
+  protected function setupValidMocks($use_ajax = self::USE_AJAX) {
     $view = $this->getMockBuilder('Drupal\views\Entity\View')
       ->disableOriginalConstructor()
       ->getMock();
@@ -314,7 +325,10 @@ class ViewAjaxControllerTest extends UnitTestCase {
     $executable->expects($this->once())
       ->method('access')
       ->will($this->returnValue(TRUE));
-    $executable->expects($this->once())
+    $executable->expects($this->any())
+      ->method('setDisplay')
+      ->willReturn(TRUE);
+    $executable->expects($this->atMost(1))
       ->method('preview')
       ->will($this->returnValue(['#markup' => 'View result']));
 
@@ -323,6 +337,28 @@ class ViewAjaxControllerTest extends UnitTestCase {
       ->with($view)
       ->will($this->returnValue($executable));
 
+    $display_handler = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase')
+      ->disableOriginalConstructor()
+      ->getMock();
+    // Ensure that the pager element is not set.
+    $display_handler->expects($this->never())
+      ->method('setOption');
+    $display_handler->expects($this->any())
+      ->method('getOption')
+      ->with('use_ajax')
+      ->willReturn($use_ajax);
+
+    $display_collection = $this->getMockBuilder('Drupal\views\DisplayPluginCollection')
+      ->disableOriginalConstructor()
+      ->getMock();
+    $display_collection->expects($this->any())
+      ->method('get')
+      ->with('page_1')
+      ->will($this->returnValue($display_handler));
+
+    $executable->display_handler = $display_handler;
+    $executable->displayHandlers = $display_collection;
+
     return [$view, $executable];
   }