summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2015-05-07 00:26:51 (GMT)
committerAlex Pott2015-05-07 00:26:51 (GMT)
commit40d6c147c54fef87cf94e5ff52f302f005afd186 (patch)
treec2d087362c9423dd88dd0717f427385a7ff2dcf2
parentd04b472386a0882808e2629ace92248aecb7df97 (diff)
Issue #2208811 by Pol, ivanjaros, dawehner, wizonesolutions: views_embed_view() cannot handle arguments
-rw-r--r--core/modules/views/src/Tests/ModuleTest.php67
-rw-r--r--core/modules/views/src/Tests/ViewUnitTestBase.php2
-rw-r--r--core/modules/views/src/ViewExecutable.php6
-rw-r--r--core/modules/views/tests/modules/views_test_config/test_views/views.view.test_argument.yml129
4 files changed, 200 insertions, 4 deletions
diff --git a/core/modules/views/src/Tests/ModuleTest.php b/core/modules/views/src/Tests/ModuleTest.php
index e237a19..04cb9cd 100644
--- a/core/modules/views/src/Tests/ModuleTest.php
+++ b/core/modules/views/src/Tests/ModuleTest.php
@@ -23,7 +23,7 @@ class ModuleTest extends ViewUnitTestBase {
*
* @var array
*/
- public static $testViews = array('test_view_status', 'test_view');
+ public static $testViews = array('test_view_status', 'test_view', 'test_argument');
/**
* Modules to enable.
@@ -269,6 +269,71 @@ class ModuleTest extends ViewUnitTestBase {
}
/**
+ * Tests views.module: views_embed_view().
+ */
+ public function testViewsEmbedView() {
+ $this->enableModules(array('user'));
+
+ $result = views_embed_view('test_argument');
+ $this->assertEqual(count($result['#view']->result), 5);
+
+ $result = views_embed_view('test_argument', 'default', 1);
+ $this->assertEqual(count($result['#view']->result), 1);
+
+ $result = views_embed_view('test_argument', 'default', '1,2');
+ $this->assertEqual(count($result['#view']->result), 2);
+
+ $result = views_embed_view('test_argument', 'default', '1,2', 'John');
+ $this->assertEqual(count($result['#view']->result), 1);
+
+ $result = views_embed_view('test_argument', 'default', '1,2', 'John,George');
+ $this->assertEqual(count($result['#view']->result), 2);
+ }
+
+ /**
+ * Tests the \Drupal\views\ViewsExecutable::preview() method.
+ */
+ public function testViewsPreview() {
+ $this->enableModules(array('user'));
+
+ $view = Views::getView('test_argument');
+ $result = $view->preview('default');
+ $this->assertEqual(count($result['#view']->result), 5);
+
+ $view = Views::getView('test_argument');
+ $result = $view->preview('default', array('0' => 1));
+ $this->assertEqual(count($result['#view']->result), 1);
+
+ $view = Views::getView('test_argument');
+ $result = $view->preview('default', array('3' => 1));
+ $this->assertEqual(count($result['#view']->result), 1);
+
+ $view = Views::getView('test_argument');
+ $result = $view->preview('default', array('0' => '1,2'));
+ $this->assertEqual(count($result['#view']->result), 2);
+
+ $view = Views::getView('test_argument');
+ $result = $view->preview('default', array('3' => '1,2'));
+ $this->assertEqual(count($result['#view']->result), 2);
+
+ $view = Views::getView('test_argument');
+ $result = $view->preview('default', array('0' => '1,2', '1' => 'John'));
+ $this->assertEqual(count($result['#view']->result), 1);
+
+ $view = Views::getView('test_argument');
+ $result = $view->preview('default', array('3' => '1,2', '4' => 'John'));
+ $this->assertEqual(count($result['#view']->result), 1);
+
+ $view = Views::getView('test_argument');
+ $result = $view->preview('default', array('0' => '1,2', '1' => 'John,George'));
+ $this->assertEqual(count($result['#view']->result), 2);
+
+ $view = Views::getView('test_argument');
+ $result = $view->preview('default', array('3' => '1,2', '4' => 'John,George'));
+ $this->assertEqual(count($result['#view']->result), 2);
+ }
+
+ /**
* Helper to return an expected views option array.
*
* @param array $views
diff --git a/core/modules/views/src/Tests/ViewUnitTestBase.php b/core/modules/views/src/Tests/ViewUnitTestBase.php
index 5efbc14..3aba1f5 100644
--- a/core/modules/views/src/Tests/ViewUnitTestBase.php
+++ b/core/modules/views/src/Tests/ViewUnitTestBase.php
@@ -118,7 +118,7 @@ abstract class ViewUnitTestBase extends KernelTestBase {
* @param array $args
* (optional) An array of the view arguments to use for the view.
*/
- protected function executeView($view, $args = array()) {
+ protected function executeView($view, array $args = array()) {
$view->setDisplay();
$view->preExecute($args);
$view->execute();
diff --git a/core/modules/views/src/ViewExecutable.php b/core/modules/views/src/ViewExecutable.php
index 71b37c8..d44f477 100644
--- a/core/modules/views/src/ViewExecutable.php
+++ b/core/modules/views/src/ViewExecutable.php
@@ -483,8 +483,10 @@ class ViewExecutable implements \Serializable {
* Set the arguments that come to this view. Usually from the URL
* but possibly from elsewhere.
*/
- public function setArguments($args) {
- $this->args = $args;
+ public function setArguments(array $args) {
+ // The array keys of the arguments will be incorrect if set by
+ // views_embed_view() or \Drupal\views\ViewExecutable:preview().
+ $this->args = array_values($args);
}
/**
diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_argument.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_argument.yml
new file mode 100644
index 0000000..bdff076
--- /dev/null
+++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_argument.yml
@@ -0,0 +1,129 @@
+langcode: en
+status: true
+dependencies: { }
+label: ''
+module: views
+description: ''
+tag: ''
+base_table: views_test_data
+base_field: nid
+core: '8'
+id: test_argument
+display:
+ default:
+ display_options:
+ defaults:
+ fields: false
+ pager: false
+ sorts: false
+ arguments: false
+ fields:
+ age:
+ field: age
+ id: age
+ relationship: none
+ table: views_test_data
+ id:
+ field: id
+ id: id
+ relationship: none
+ table: views_test_data
+ name:
+ field: name
+ id: name
+ relationship: none
+ table: views_test_data
+ pager:
+ options:
+ offset: 0
+ type: none
+ sorts:
+ id:
+ field: id
+ id: id
+ order: ASC
+ relationship: none
+ table: views_test_data
+ arguments:
+ id:
+ id: id
+ table: views_test_data
+ field: id
+ relationship: none
+ group_type: group
+ admin_label: ''
+ default_action: ignore
+ exception:
+ value: all
+ title_enable: false
+ title: All
+ title_enable: false
+ title: ''
+ default_argument_type: fixed
+ default_argument_options:
+ argument: ''
+ default_argument_skip_url: false
+ summary_options:
+ base_path: ''
+ count: true
+ items_per_page: 25
+ override: false
+ summary:
+ sort_order: asc
+ number_of_records: 0
+ format: default_summary
+ specify_validation: false
+ validate:
+ type: none
+ fail: 'not found'
+ validate_options: { }
+ break_phrase: true
+ not: false
+ entity_type: node
+ entity_field: nid
+ plugin_id: numeric
+ name:
+ id: title
+ table: views_test_data
+ field: name
+ relationship: none
+ group_type: group
+ admin_label: ''
+ default_action: ignore
+ exception:
+ value: all
+ title_enable: false
+ title: All
+ title_enable: false
+ title: ''
+ default_argument_type: fixed
+ default_argument_options:
+ argument: ''
+ default_argument_skip_url: false
+ summary_options:
+ base_path: ''
+ count: true
+ items_per_page: 25
+ override: false
+ summary:
+ sort_order: asc
+ number_of_records: 0
+ format: default_summary
+ specify_validation: false
+ validate:
+ type: none
+ fail: 'not found'
+ validate_options: { }
+ glossary: false
+ limit: 0
+ case: none
+ path_case: none
+ transform_dash: false
+ break_phrase: true
+ entity_type: node
+ entity_field: name
+ plugin_id: string
+ display_plugin: default
+ display_title: Master
+ id: default
+ position: 0