summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2013-06-17 22:04:25 (GMT)
committerAlex Pott2013-06-17 22:04:25 (GMT)
commitbf1b3d225593b69484c3f7fa6175da93e01ee4ea (patch)
treec06eb089bae8136b2fac623907d8bd49f3f57056
parent614550dcd212ec134d94d974c3305eb9c1efb1ab (diff)
Issue #1830828 by dawehner, Pancho: Fixed view result counter field.
-rw-r--r--core/modules/views/lib/Drupal/views/Plugin/views/HandlerBase.php11
-rw-r--r--core/modules/views/lib/Drupal/views/Plugin/views/field/Counter.php2
-rw-r--r--core/modules/views/lib/Drupal/views/Tests/Handler/FieldCounterTest.php105
-rw-r--r--core/modules/views/tests/Drupal/views/Tests/Plugin/field/CounterTest.php248
4 files changed, 256 insertions, 110 deletions
diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/HandlerBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/HandlerBase.php
index bceb076..b026438 100644
--- a/core/modules/views/lib/Drupal/views/Plugin/views/HandlerBase.php
+++ b/core/modules/views/lib/Drupal/views/Plugin/views/HandlerBase.php
@@ -7,7 +7,10 @@
namespace Drupal\views\Plugin\views;
+use Drupal\Component\Utility\String;
use Drupal\Component\Utility\Unicode;
+use Drupal\Component\Utility\UrlValidator;
+use Drupal\Component\Utility\Xss;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\PluginBase;
use Drupal\views\ViewExecutable;
@@ -212,16 +215,16 @@ abstract class HandlerBase extends PluginBase {
public function sanitizeValue($value, $type = NULL) {
switch ($type) {
case 'xss':
- $value = filter_xss($value);
+ $value = Xss::filter($value);
break;
case 'xss_admin':
- $value = filter_xss_admin($value);
+ $value = Xss::filterAdmin($value);
break;
case 'url':
- $value = check_url($value);
+ $value = String::checkPlain(UrlValidator::stripDangerousProtocols($value));
break;
default:
- $value = check_plain($value);
+ $value = String::checkPlain($value);
break;
}
return $value;
diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/field/Counter.php b/core/modules/views/lib/Drupal/views/Plugin/views/field/Counter.php
index f23f20f..15b041c 100644
--- a/core/modules/views/lib/Drupal/views/Plugin/views/field/Counter.php
+++ b/core/modules/views/lib/Drupal/views/Plugin/views/field/Counter.php
@@ -50,7 +50,7 @@ class Counter extends FieldPluginBase {
$pager = $this->view->pager;
// Get the base count of the pager.
if ($pager->usePager()) {
- $count += ($pager->getItemsPerPage() * $pager->getCurrentPage() + $pager->setOffset());
+ $count += ($pager->getItemsPerPage() * $pager->getCurrentPage() + $pager->getOffset());
}
// Add the counter for the current site.
$count += $this->view->row_index + 1;
diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldCounterTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldCounterTest.php
deleted file mode 100644
index cddff8b..0000000
--- a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldCounterTest.php
+++ /dev/null
@@ -1,105 +0,0 @@
-<?php
-
-/**
- * @file
- * Definition of Drupal\views\Tests\Handler\FieldCounterTest.
- */
-
-namespace Drupal\views\Tests\Handler;
-
-use Drupal\views\Tests\ViewUnitTestBase;
-
-/**
- * Tests the Drupal\views\Plugin\views\field\Counter handler.
- */
-class FieldCounterTest extends ViewUnitTestBase {
-
- /**
- * Modules to enable.
- *
- * @var array
- */
- public static $modules = array('user');
-
- /**
- * Views used by this test.
- *
- * @var array
- */
- public static $testViews = array('test_view');
-
- public static function getInfo() {
- return array(
- 'name' => 'Field: Counter',
- 'description' => 'Tests the Drupal\views\Plugin\views\field\Counter handler.',
- 'group' => 'Views Handlers',
- );
- }
-
- protected function setUp() {
- parent::setUp();
- $this->installSchema('user', 'role_permission');
- }
-
- function testSimple() {
- $view = views_get_view('test_view');
- $view->setDisplay();
- $view->displayHandlers->get('default')->overrideOption('fields', array(
- 'counter' => array(
- 'id' => 'counter',
- 'table' => 'views',
- 'field' => 'counter',
- 'relationship' => 'none',
- ),
- 'name' => array(
- 'id' => 'name',
- 'table' => 'views_test_data',
- 'field' => 'name',
- 'relationship' => 'none',
- ),
- ));
- $view->preview();
-
- $counter = $view->style_plugin->getField(0, 'counter');
- $this->assertEqual($counter, 1, format_string('Make sure the expected number (@expected) patches with the rendered number (@counter)', array('@expected' => 1, '@counter' => $counter)));
- $counter = $view->style_plugin->getField(1, 'counter');
- $this->assertEqual($counter, 2, format_string('Make sure the expected number (@expected) patches with the rendered number (@counter)', array('@expected' => 2, '@counter' => $counter)));
- $counter = $view->style_plugin->getField(2, 'counter');
- $this->assertEqual($counter, 3, format_string('Make sure the expected number (@expected) patches with the rendered number (@counter)', array('@expected' => 3, '@counter' => $counter)));
- $view->destroy();
-
- $view->setDisplay();
- $rand_start = rand(5, 10);
- $view->displayHandlers->get('default')->overrideOption('fields', array(
- 'counter' => array(
- 'id' => 'counter',
- 'table' => 'views',
- 'field' => 'counter',
- 'relationship' => 'none',
- 'counter_start' => $rand_start
- ),
- 'name' => array(
- 'id' => 'name',
- 'table' => 'views_test_data',
- 'field' => 'name',
- 'relationship' => 'none',
- ),
- ));
- $view->preview();
-
- $counter = $view->style_plugin->getField(0, 'counter');
- $expected_number = 0 + $rand_start;
- $this->assertEqual($counter, $expected_number, format_string('Make sure the expected number (@expected) patches with the rendered number (@counter)', array('@expected' => $expected_number, '@counter' => $counter)));
- $counter = $view->style_plugin->getField(1, 'counter');
- $expected_number = 1 + $rand_start;
- $this->assertEqual($counter, $expected_number, format_string('Make sure the expected number (@expected) patches with the rendered number (@counter)', array('@expected' => $expected_number, '@counter' => $counter)));
- $counter = $view->style_plugin->getField(2, 'counter');
- $expected_number = 2 + $rand_start;
- $this->assertEqual($counter, $expected_number, format_string('Make sure the expected number (@expected) patches with the rendered number (@counter)', array('@expected' => $expected_number, '@counter' => $counter)));
- }
-
- // @TODO: Write tests for pager.
- function testPager() {
- }
-
-}
diff --git a/core/modules/views/tests/Drupal/views/Tests/Plugin/field/CounterTest.php b/core/modules/views/tests/Drupal/views/Tests/Plugin/field/CounterTest.php
new file mode 100644
index 0000000..8663881
--- /dev/null
+++ b/core/modules/views/tests/Drupal/views/Tests/Plugin/field/CounterTest.php
@@ -0,0 +1,248 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\views\Tests\Plugin\field\CounterTest.
+ */
+
+namespace Drupal\views\Tests\Plugin\field;
+
+use Drupal\Component\Utility\String;
+use Drupal\Tests\UnitTestCase;
+use Drupal\views\Plugin\Core\Entity\View;
+use Drupal\views\Plugin\views\field\Counter;
+use Drupal\views\Tests\ViewTestData;
+
+/**
+ * Tests the counter field plugin.
+ *
+ * @see \Drupal\views\Plugin\views\field\Counter
+ */
+class CounterTest extends UnitTestCase {
+
+ /**
+ * The pager plugin instance.
+ *
+ * @var \Drupal\views\Plugin\views\pager\PagerPluginBase
+ */
+ protected $pager;
+
+ /**
+ * The view executable.
+ *
+ * @var \Drupal\views\ViewExecutable
+ */
+ protected $view;
+
+ /**
+ * The display plugin instance.
+ *
+ * @var \Drupal\views\Plugin\views\display\DisplayPluginBase
+ */
+ protected $display;
+
+
+ /**
+ * Stores the test data.
+ *
+ * @var array
+ */
+ protected $testData;
+
+ /**
+ * The handler definition of the counter field.
+ *
+ * @return array
+ */
+ protected $definition;
+
+ public static function getInfo() {
+ return array(
+ 'name' => 'Field: Counter (Unit)',
+ 'description' => 'Tests the \Drupal\views\Plugin\views\field\Counter handler.',
+ 'group' => 'Views Handlers',
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function setUp() {
+ parent::setUp();
+
+ // Setup basic stuff like the view and the display.
+ $config = array();
+ $config['display']['default'] = array(
+ 'id' => 'default',
+ 'display_plugin' => 'default',
+ 'display_title' => 'Default',
+ );
+
+ $storage = new View($config, 'view');
+ $this->view = $this->getMock('Drupal\views\ViewExecutable', NULL, array($storage));
+
+ $this->display = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->pager = $this->getMockBuilder('Drupal\views\Plugin\views\pager\Full')
+ ->disableOriginalConstructor()
+ ->setMethods(NULL)
+ ->getMock();
+
+ $this->view->display_handler = $this->display;
+ $this->view->pager = $this->pager;
+
+
+ $this->testData = ViewTestData::dataSet();
+
+ $this->definition = array('title' => 'counter field', 'plugin_type' => 'field');
+ }
+
+
+ /**
+ * Provides some row index to test.
+ *
+ * @return array
+ * Returns an array of row index to test.
+ */
+ public function providerRowIndexes() {
+ return array(
+ array(0),
+ array(1),
+ array(2),
+ );
+ }
+
+ /**
+ * Tests a simple counter field.
+ *
+ * @dataProvider providerRowIndexes
+ */
+ public function testSimpleCounter($i) {
+ $counter_handler = new Counter(array(), 'counter', $this->definition);
+ $options = array();
+ $counter_handler->init($this->view, $this->display, $options);
+
+ $this->view->row_index = $i;
+ $expected = $i + 1;
+
+ $counter = $counter_handler->getValue($this->testData[$i]);
+ $this->assertEquals($expected, $counter, String::format('The expected number (@expected) patches with the rendered number (@counter) failed', array(
+ '@expected' => $expected,
+ '@counter' => $counter
+ )));
+ $counter = $counter_handler->render($this->testData[$i]);
+ $this->assertEquals($expected, $counter_handler->render($this->testData[$i]), String::format('The expected number (@expected) patches with the rendered number (@counter) failed', array(
+ '@expected' => $expected,
+ '@counter' => $counter
+ )));
+ }
+
+ /**
+ * Tests a counter with a random start.
+ *
+ * @param int $i
+ * The row index to test.
+ *
+ * @dataProvider providerRowIndexes
+ */
+ public function testCounterRandomStart($i) {
+ // Setup a counter field with a random start.
+ $rand_start = rand(5, 10);
+ $counter_handler = new Counter(array(), 'counter', $this->definition);
+ $options = array(
+ 'counter_start' => $rand_start,
+ );
+ $counter_handler->init($this->view, $this->display, $options);
+
+ $this->view->row_index = $i;
+ $expected = $rand_start + $i;
+
+ $counter = $counter_handler->getValue($this->testData[$i]);
+ $this->assertEquals($expected, $counter, String::format('The expected number (@expected) patches with the rendered number (@counter) failed', array(
+ '@expected' => $expected,
+ '@counter' => $counter
+ )));
+ $counter = $counter_handler->render($this->testData[$i]);
+ $this->assertEquals($expected, $counter_handler->render($this->testData[$i]), String::format('The expected number (@expected) patches with the rendered number (@counter) failed', array(
+ '@expected' => $expected,
+ '@counter' => $counter
+ )));
+ }
+
+ /**
+ * Tests a counter field with a random pager offset.
+ *
+ * @param int $i
+ * The row index to test.
+ *
+ * @dataProvider providerRowIndexes
+ */
+ public function testCounterRandomPagerOffset($i) {
+ // Setup a counter field with a pager with a random offset.
+ $offset = 3;
+ $this->pager->setOffset($offset);
+
+ $rand_start = rand(5, 10);
+ $counter_handler = new Counter(array(), 'counter', $this->definition);
+ $options = array(
+ 'counter_start' => $rand_start,
+ );
+ $counter_handler->init($this->view, $this->display, $options);
+
+ $this->view->row_index = $i;
+ $expected = $offset + $rand_start + $i;
+
+ $counter = $counter_handler->getValue($this->testData[$i]);
+ $this->assertEquals($expected, $counter, String::format('The expected number (@expected) patches with the rendered number (@counter) failed', array(
+ '@expected' => $expected,
+ '@counter' => $counter
+ )));
+ $counter = $counter_handler->render($this->testData[$i]);
+ $this->assertEquals($expected, $counter_handler->render($this->testData[$i]), String::format('The expected number (@expected) patches with the rendered number (@counter) failed', array(
+ '@expected' => $expected,
+ '@counter' => $counter
+ )));
+ }
+
+ /**
+ * Tests a counter field on the second page.
+ *
+ * @param int $i
+ * The row index to test.
+ *
+ * @dataProvider providerRowIndexes
+ */
+ public function testCounterSecondPage($i) {
+ $offset = 3;
+ // Setup a pager on the second page.
+ $this->pager->setOffset($offset);
+ $items_per_page = 5;
+ $this->pager->setItemsPerPage($items_per_page);
+ $current_page = 1;
+ $this->pager->setCurrentPage($current_page);
+
+ $rand_start = rand(5, 10);
+ $counter_handler = new Counter(array(), 'counter', $this->definition);
+ $options = array(
+ 'counter_start' => $rand_start,
+ );
+ $counter_handler->init($this->view, $this->display, $options);
+
+ $this->view->row_index = $i;
+ $expected = $items_per_page + $offset + $rand_start + $i;
+
+ $counter = $counter_handler->getValue($this->testData[$i]);
+ $this->assertEquals($expected, $counter, String::format('The expected number (@expected) patches with the rendered number (@counter) failed', array(
+ '@expected' => $expected,
+ '@counter' => $counter
+ )));
+ $counter = $counter_handler->render($this->testData[$i]);
+ $this->assertEquals($expected, $counter_handler->render($this->testData[$i]), String::format('The expected number (@expected) patches with the rendered number (@counter) failed', array(
+ '@expected' => $expected,
+ '@counter' => $counter
+ )));
+ }
+
+}