From 525e58aa720eec4137f8ee9b5a8de43235531498 Mon Sep 17 00:00:00 2001
From: Mariano D'Agostino <dagmar@154086.no-reply.drupal.org>
Date: Wed, 12 Nov 2014 17:08:36 +0000
Subject: [PATCH] Issue #1174588 by dagmar, dawehner | David D: Fixed
 Overriding 'Display access settings' changes all displays
 unless 'Access control type' is also overridden.

---
 includes/admin.inc               |    5 +++
 plugins/views_plugin_display.inc |    7 +++-
 tests/views_ui.test              |   69 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 80 insertions(+), 1 deletion(-)

diff --git a/includes/admin.inc b/includes/admin.inc
index 9db5854..1f7a245 100644
--- a/includes/admin.inc
+++ b/includes/admin.inc
@@ -2650,6 +2650,11 @@ function views_ui_standard_display_dropdown(&$form, &$form_state, $section) {
     return;
   }
 
+  $defaultable_sections = $current_display->handler->defaultable_sections($section);
+  if (!empty($defaultable_sections)) {
+    $section = array_shift($defaultable_sections);
+  }
+
   // Determine whether any other displays have overrides for this section.
   $section_overrides = FALSE;
   $section_defaulted = $current_display->handler->is_defaulted($section);
diff --git a/plugins/views_plugin_display.inc b/plugins/views_plugin_display.inc
index db124de..0efabe6 100644
--- a/plugins/views_plugin_display.inc
+++ b/plugins/views_plugin_display.inc
@@ -2474,7 +2474,12 @@ class views_plugin_display extends views_plugin {
    * If override/revert was clicked, perform the proper toggle.
    */
   function options_override($form, &$form_state) {
-    $this->set_override($form_state['section']);
+    $section = $form_state['section'];
+    $defaultable_sections = $this->defaultable_sections($section);
+    if (!empty($defaultable_sections)) {
+      $section = array_shift($defaultable_sections);
+    }
+    $this->set_override($section);
   }
 
   /**
diff --git a/tests/views_ui.test b/tests/views_ui.test
index 8785539..f308e3f 100644
--- a/tests/views_ui.test
+++ b/tests/views_ui.test
@@ -970,4 +970,73 @@ class ViewsUIWizardOverrideDisplaysTestCase extends ViewsUIWizardHelper {
     $this->drupalPost("admin/structure/views/view/{$view['name']}/edit/block", array(), t('Save'));
     $this->assertText($view['page[title]']);
   }
+
+  /**
+   * Test multiple overridden page displays with different permissions.
+   */
+  function testPageDisplaysWithDifferentPermissions() {
+    // Create a basic view with no displays.
+    $view['human_name'] = $this->randomName(16);
+    $view['name'] = strtolower($this->randomName(16));
+    $view['page[create]'] = FALSE;
+    $view['block[create]'] = FALSE;
+
+    $this->drupalPost('admin/structure/views/add', $view, t('Continue & edit'));
+
+    // Add 4 page displays. Using different permissions for each one.
+    $displays = array(
+      'page_1' => 'administer modules',
+      'page_2' => 'administer nodes',
+      'page_3' => 'access content',
+      'page_4' => 'bypass node access',
+    );
+
+    foreach ($displays as $display => $permission) {
+      // Add a new Page, Set it permissions to 'administer modules'.
+      $edit = array();
+      $this->drupalPost("admin/structure/views/view/{$view['name']}/edit", $edit, t('Add Page'));
+
+      $edit = array(
+        'path' => "my-path",
+      );
+      $this->drupalPost("admin/structure/views/nojs/display/{$view['name']}/$display/path", $edit, t('Apply'));
+
+      $edit = array(
+        'override[dropdown]' => $display,
+        'access_options[perm]' => $permission,
+      );
+      $this->drupalPost("admin/structure/views/nojs/display/{$view['name']}/$display/access_options", $edit, t('Apply'));
+    }
+
+    // Save all the displays.
+    $this->drupalPost("admin/structure/views/view/{$view['name']}/edit/page_4", array(), t('Save'));
+
+    foreach ($displays as $display => $permission) {
+      $this->drupalGet("admin/structure/views/nojs/display/{$view['name']}/$display/access_options");
+      // Check the permissions are the same after save all the displays.
+      $this->assertOptionSelected('edit-access-options-perm', $permission);
+    }
+
+    // Now set page_1, page_2, and page_4 to defaults
+    foreach (array('page_1', 'page_2', 'page_4') as $display) {
+      $edit = array(
+        'override[dropdown]' => 'default',
+        'access_options[perm]' => 'administer nodes',
+      );
+      $this->drupalPost("admin/structure/views/nojs/display/{$view['name']}/$display/access_options", $edit, t('Apply'));
+    }
+
+    // Save all the displays
+    $this->drupalPost("admin/structure/views/view/{$view['name']}/edit/page_4", array(), t('Save'));
+
+    // Finally check that only 'page_3' have a different permission assigned.
+    foreach (array('page_1', 'page_2', 'page_3', 'page_4') as $display) {
+      $this->drupalGet("admin/structure/views/nojs/display/{$view['name']}/$display/access_options");
+      $permission = 'administer nodes';
+      if ($display == 'page_3') {
+        $permission = 'access content';
+      }
+      $this->assertOptionSelected('edit-access-options-perm', $permission);
+    }
+  }
 }
-- 
1.7.10.4

