FieldCounterTest.php 3.46 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1
2
3
4
<?php

/**
 * @file
5
 * Definition of Drupal\views\Tests\Handler\FieldCounterTest.
merlinofchaos's avatar
merlinofchaos committed
6
7
 */

8
9
namespace Drupal\views\Tests\Handler;

10
11
use Drupal\views\Tests\ViewUnitTestBase;

merlinofchaos's avatar
merlinofchaos committed
12
/**
aspilicious's avatar
aspilicious committed
13
 * Tests the Drupal\views\Plugin\views\field\Counter handler.
merlinofchaos's avatar
merlinofchaos committed
14
 */
15
class FieldCounterTest extends ViewUnitTestBase {
16

17
18
19
20
21
22
23
  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = array('test_view');

merlinofchaos's avatar
merlinofchaos committed
24
25
26
  public static function getInfo() {
    return array(
      'name' => 'Field: Counter',
aspilicious's avatar
aspilicious committed
27
      'description' => 'Tests the Drupal\views\Plugin\views\field\Counter handler.',
merlinofchaos's avatar
merlinofchaos committed
28
29
30
31
      'group' => 'Views Handlers',
    );
  }

32
33
34
  protected function setUp() {
    parent::setUp();

35
    $this->enableModules(array('user'));
36
37
  }

merlinofchaos's avatar
merlinofchaos committed
38
  function testSimple() {
39
40
    $view = views_get_view('test_view');
    $view->setDisplay();
41
    $view->displayHandlers['default']->overrideOption('fields', array(
merlinofchaos's avatar
merlinofchaos committed
42
43
44
45
46
47
48
49
      'counter' => array(
        'id' => 'counter',
        'table' => 'views',
        'field' => 'counter',
        'relationship' => 'none',
      ),
      'name' => array(
        'id' => 'name',
50
        'table' => 'views_test_data',
merlinofchaos's avatar
merlinofchaos committed
51
52
53
54
55
56
        'field' => 'name',
        'relationship' => 'none',
      ),
    ));
    $view->preview();

57
    $counter = $view->style_plugin->get_field(0, 'counter');
58
    $this->assertEqual($counter, 1, format_string('Make sure the expected number (@expected) patches with the rendered number (@counter)', array('@expected' => 1, '@counter' => $counter)));
59
    $counter = $view->style_plugin->get_field(1, 'counter');
60
    $this->assertEqual($counter, 2, format_string('Make sure the expected number (@expected) patches with the rendered number (@counter)', array('@expected' => 2, '@counter' => $counter)));
61
    $counter = $view->style_plugin->get_field(2, 'counter');
62
    $this->assertEqual($counter, 3, format_string('Make sure the expected number (@expected) patches with the rendered number (@counter)', array('@expected' => 3, '@counter' => $counter)));
merlinofchaos's avatar
merlinofchaos committed
63
64
    $view->destroy();

65
    $view->setDisplay();
merlinofchaos's avatar
merlinofchaos committed
66
    $rand_start = rand(5, 10);
67
    $view->displayHandlers['default']->overrideOption('fields', array(
merlinofchaos's avatar
merlinofchaos committed
68
69
70
71
72
73
74
75
76
      'counter' => array(
        'id' => 'counter',
        'table' => 'views',
        'field' => 'counter',
        'relationship' => 'none',
        'counter_start' => $rand_start
      ),
      'name' => array(
        'id' => 'name',
77
        'table' => 'views_test_data',
merlinofchaos's avatar
merlinofchaos committed
78
79
80
81
82
83
        'field' => 'name',
        'relationship' => 'none',
      ),
    ));
    $view->preview();

84
    $counter = $view->style_plugin->get_field(0, 'counter');
85
86
    $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)));
87
    $counter = $view->style_plugin->get_field(1, 'counter');
88
89
    $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)));
90
    $counter = $view->style_plugin->get_field(2, 'counter');
91
92
    $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)));
merlinofchaos's avatar
merlinofchaos committed
93
94
95
96
97
  }

  // @TODO: Write tests for pager.
  function testPager() {
  }
98

merlinofchaos's avatar
merlinofchaos committed
99
}