From eca2c2f79ba9444992832643f62d985b5fdd1abe Mon Sep 17 00:00:00 2001 From: webchick <webchick@24967.no-reply.drupal.org> Date: Sun, 17 Mar 2013 00:30:02 -0700 Subject: [PATCH] Issue #1934558 by dawehner, olli: Create a test for views date handlers. --- .../views/Plugin/views/argument/Date.php | 1 + .../views/Tests/Handler/ArgumentDateTest.php | 319 ++++++++++++++++++ .../views.view.test_argument_date.yml | 105 ++++++ 3 files changed, 425 insertions(+) create mode 100644 core/modules/views/lib/Drupal/views/Tests/Handler/ArgumentDateTest.php create mode 100644 core/modules/views/tests/views_test_config/test_views/views.view.test_argument_date.yml diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/argument/Date.php b/core/modules/views/lib/Drupal/views/Plugin/views/argument/Date.php index 57638f9bef5d..59b87de887b5 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/argument/Date.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/argument/Date.php @@ -8,6 +8,7 @@ namespace Drupal\views\Plugin\views\argument; use Drupal\Component\Annotation\Plugin; +use Drupal\Core\Database\Database; /** * Abstract argument handler for dates. diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/ArgumentDateTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/ArgumentDateTest.php new file mode 100644 index 000000000000..2ad226a0222d --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/ArgumentDateTest.php @@ -0,0 +1,319 @@ +<?php + +/** + * @file + * Contains \Drupal\views\Tests\Handler\ArgumentDateTest. + */ + +namespace Drupal\views\Tests\Handler; + +use Drupal\views\Tests\ViewUnitTestBase; + +/** + * Tests the core date argument handlers. + * + * @see \Drupal\views\Plugin\views\argument\Date + */ +class ArgumentDateTest extends ViewUnitTestBase { + + /** + * Views used by this test. + * + * @var array + */ + public static $testViews = array('test_argument_date'); + + /** + * Modules to enable. + * + * @todo Remove the node dependency once the handlers are moved to views. + * + * @var array + */ + public static $modules = array('node', 'user'); + + /** + * Stores the column map for this testCase. + * + * @var array + */ + protected $columnMap = array( + 'id' => 'id', + ); + + public static function getInfo() { + return array( + 'name' => 'Argument: Date', + 'description' => 'Tests the core date argument handler.', + 'group' => 'Views Handlers', + ); + } + + protected function setUp() { + parent::setUp(); + + $this->installSchema('user', 'role_permission'); + } + + + /** + * Overrides \Drupal\views\Tests\ViewUnitTestBase::viewsData(). + */ + public function viewsData() { + $data = parent::viewsData(); + + $date_plugins = array( + 'node_created_fulldate', + 'node_created_day', + 'node_created_month', + 'node_created_week', + 'node_created_year', + 'node_created_year_month', + ); + foreach ($date_plugins as $plugin_id) { + $data['views_test_data'][$plugin_id] = $data['views_test_data']['created']; + $data['views_test_data'][$plugin_id]['real field'] = 'created'; + $data['views_test_data'][$plugin_id]['argument']['id'] = $plugin_id; + } + return $data; + } + + /** + * Tests the CreatedFullDate handler. + * + * @see \Drupal\node\Plugin\views\argument\CreatedFullDate + */ + public function testCreatedFullDateHandler() { + $view = views_get_view('test_argument_date'); + $view->setDisplay('default'); + $this->executeView($view, array('20000102')); + $expected = array(); + $expected[] = array('id' => 2); + $this->assertIdenticalResultset($view, $expected, $this->columnMap); + $view->destroy(); + + $view->setDisplay('default'); + $this->executeView($view, array('20000101')); + $expected = array(); + $expected[] = array('id' => 1); + $expected[] = array('id' => 3); + $expected[] = array('id' => 4); + $expected[] = array('id' => 5); + $this->assertIdenticalResultset($view, $expected, $this->columnMap); + $view->destroy(); + + $view->setDisplay('default'); + $this->executeView($view, array('20001023')); + $expected = array(); + $this->assertIdenticalResultset($view, $expected, $this->columnMap); + $view->destroy(); + } + + /** + * Tests the Day handler. + * + * @see \Drupal\node\Plugin\views\argument\CreatedDay + */ + public function testDayHandler() { + $view = views_get_view('test_argument_date'); + $view->setDisplay('embed_1'); + $this->executeView($view, array('02')); + $expected = array(); + $expected[] = array('id' => 2); + $this->assertIdenticalResultset($view, $expected, $this->columnMap); + $view->destroy(); + + $view->setDisplay('embed_1'); + $this->executeView($view, array('01')); + $expected = array(); + $expected[] = array('id' => 1); + $expected[] = array('id' => 3); + $expected[] = array('id' => 4); + $expected[] = array('id' => 5); + $this->assertIdenticalResultset($view, $expected, $this->columnMap); + $view->destroy(); + + $view->setDisplay('embed_1'); + $this->executeView($view, array('23')); + $expected = array(); + $this->assertIdenticalResultset($view, $expected, $this->columnMap); + } + + /** + * Tests the Month handler. + * + * @see \Drupal\node\Plugin\views\argument\CreatedMonth + */ + public function testMonthHandler() { + $view = views_get_view('test_argument_date'); + $view->setDisplay('embed_2'); + $this->executeView($view, array('01')); + $expected = array(); + $expected[] = array('id' => 1); + $expected[] = array('id' => 2); + $expected[] = array('id' => 3); + $expected[] = array('id' => 4); + $expected[] = array('id' => 5); + $this->assertIdenticalResultset($view, $expected, $this->columnMap); + $view->destroy(); + + $view->setDisplay('embed_2'); + $this->executeView($view, array('23')); + $expected = array(); + $this->assertIdenticalResultset($view, $expected, $this->columnMap); + } + + /** + * Tests the Week handler. + * + * @see \Drupal\node\Plugin\views\argument\CreatedWeek + */ + public function testWeekHandler() { + $this->container->get('database')->update('views_test_data') + ->fields(array('created' => gmmktime(0, 0, 0, 1, 1, 2000))) + ->condition('id', 3) + ->execute(); + + $this->container->get('database')->update('views_test_data') + ->fields(array('created' => gmmktime(0, 0, 0, 1, 10, 2000))) + ->condition('id', 4) + ->execute(); + + $this->container->get('database')->update('views_test_data') + ->fields(array('created' => gmmktime(0, 0, 0, 2, 1, 2000))) + ->condition('id', 5) + ->execute(); + + $view = views_get_view('test_argument_date'); + $view->setDisplay('embed_3'); + // The first jan 2000 was still in the last week of the previous year. + $this->executeView($view, array(52)); + $expected = array(); + $expected[] = array('id' => 1); + $expected[] = array('id' => 2); + $expected[] = array('id' => 3); + $this->assertIdenticalResultset($view, $expected, $this->columnMap); + $view->destroy(); + + $view->setDisplay('embed_3'); + $this->executeView($view, array('02')); + $expected = array(); + $expected[] = array('id' => 4); + $this->assertIdenticalResultset($view, $expected, $this->columnMap); + $view->destroy(); + + $view->setDisplay('embed_3'); + $this->executeView($view, array('05')); + $expected = array(); + $expected[] = array('id' => 5); + $this->assertIdenticalResultset($view, $expected, $this->columnMap); + $view->destroy(); + + $view->setDisplay('embed_3'); + $this->executeView($view, array('23')); + $expected = array(); + $this->assertIdenticalResultset($view, $expected, $this->columnMap); + } + + /** + * Tests the Year handler. + * + * @see \Drupal\node\Plugin\views\argument\CreatedYear + */ + public function testYearHandler() { + $this->container->get('database')->update('views_test_data') + ->fields(array('created' => gmmktime(0, 0, 0, 1, 1, 2001))) + ->condition('id', 3) + ->execute(); + + $this->container->get('database')->update('views_test_data') + ->fields(array('created' => gmmktime(0, 0, 0, 1, 1, 2002))) + ->condition('id', 4) + ->execute(); + + $this->container->get('database')->update('views_test_data') + ->fields(array('created' => gmmktime(0, 0, 0, 1, 1, 2002))) + ->condition('id', 5) + ->execute(); + + $view = views_get_view('test_argument_date'); + $view->setDisplay('embed_4'); + $this->executeView($view, array('2000')); + $expected = array(); + $expected[] = array('id' => 1); + $expected[] = array('id' => 2); + $this->assertIdenticalResultset($view, $expected, $this->columnMap); + $view->destroy(); + + $view->setDisplay('embed_4'); + $this->executeView($view, array('2001')); + $expected = array(); + $expected[] = array('id' => 3); + $this->assertIdenticalResultset($view, $expected, $this->columnMap); + $view->destroy(); + + $view->setDisplay('embed_4'); + $this->executeView($view, array('2002')); + $expected = array(); + $expected[] = array('id' => 4); + $expected[] = array('id' => 5); + $this->assertIdenticalResultset($view, $expected, $this->columnMap); + $view->destroy(); + + $view->setDisplay('embed_4'); + $this->executeView($view, array('23')); + $expected = array(); + $this->assertIdenticalResultset($view, $expected, $this->columnMap); + } + + /** + * Tests the YearMonth handler. + * + * @see \Drupal\node\Plugin\views\argument\CreatedYearMonth + */ + public function testYearMonthHandler() { + $this->container->get('database')->update('views_test_data') + ->fields(array('created' => gmmktime(0, 0, 0, 1, 1, 2001))) + ->condition('id', 3) + ->execute(); + + $this->container->get('database')->update('views_test_data') + ->fields(array('created' => gmmktime(0, 0, 0, 4, 1, 2001))) + ->condition('id', 4) + ->execute(); + + $this->container->get('database')->update('views_test_data') + ->fields(array('created' => gmmktime(0, 0, 0, 4, 1, 2001))) + ->condition('id', 5) + ->execute(); + + $view = views_get_view('test_argument_date'); + $view->setDisplay('embed_5'); + $this->executeView($view, array('200001')); + $expected = array(); + $expected[] = array('id' => 1); + $expected[] = array('id' => 2); + $this->assertIdenticalResultset($view, $expected, $this->columnMap); + $view->destroy(); + + $view->setDisplay('embed_5'); + $this->executeView($view, array('200101')); + $expected = array(); + $expected[] = array('id' => 3); + $this->assertIdenticalResultset($view, $expected, $this->columnMap); + $view->destroy(); + + $view->setDisplay('embed_5'); + $this->executeView($view, array('200104')); + $expected = array(); + $expected[] = array('id' => 4); + $expected[] = array('id' => 5); + $this->assertIdenticalResultset($view, $expected, $this->columnMap); + $view->destroy(); + + $view->setDisplay('embed_5'); + $this->executeView($view, array('23')); + $expected = array(); + $this->assertIdenticalResultset($view, $expected, $this->columnMap); + } +} diff --git a/core/modules/views/tests/views_test_config/test_views/views.view.test_argument_date.yml b/core/modules/views/tests/views_test_config/test_views/views.view.test_argument_date.yml new file mode 100644 index 000000000000..fe5e5445fb09 --- /dev/null +++ b/core/modules/views/tests/views_test_config/test_views/views.view.test_argument_date.yml @@ -0,0 +1,105 @@ +base_table: views_test_data +core: '8' +description: '' +status: '1' +display: + default: + display_options: + defaults: + fields: '0' + pager: '0' + pager_options: '0' + sorts: '0' + arguments: + node_created_fulldate: + field: node_created_fulldate + id: node_created_fulldate + table: views_test_data + plugin_id: node_created_fulldate + fields: + id: + field: id + id: id + relationship: none + table: views_test_data + plugin_id: numeric + pager: + options: + offset: '0' + type: none + pager_options: { } + sorts: + id: + field: id + id: id + order: ASC + relationship: none + table: views_test_data + plugin_id: numeric + display_plugin: default + display_title: Master + id: default + position: '0' + embed_1: + display_options: + defaults: + arguments: '0' + arguments: + node_created_day: + field: node_created_day + id: node_created_day + table: views_test_data + plugin_id: node_created_day + display_plugin: embed + id: embed_1 + embed_2: + display_options: + defaults: + arguments: '0' + arguments: + node_created_month: + field: node_created_month + id: node_created_month + table: views_test_data + plugin_id: node_created_month + display_plugin: embed + id: embed_2 + embed_3: + display_options: + defaults: + arguments: '0' + arguments: + node_created_week: + field: node_created_week + id: node_created_week + table: views_test_data + plugin_id: node_created_week + display_plugin: embed + id: embed_3 + embed_4: + display_options: + defaults: + arguments: '0' + arguments: + node_created_year: + field: node_created_year + id: node_created_year + table: views_test_data + plugin_id: node_created_year + display_plugin: embed + id: embed_4 + embed_5: + display_options: + defaults: + arguments: '0' + arguments: + node_created_year_month: + field: node_created_year_month + id: node_created_year_month + table: views_test_data + plugin_id: node_created_year_month + display_plugin: embed + id: embed_5 +human_name: '' +id: test_argument_date +tag: '' -- GitLab