Project

General

Profile

Paste
Download (4.22 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / tests / handlers / views_handler_field_boolean.test @ 4003efde

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of ViewsHandlerFieldBooleanTest.
6
 */
7

    
8
/**
9
 * Tests the core views_handler_field_boolean handler.
10
 */
11
class ViewsHandlerFieldBooleanTest extends ViewsSqlTest {
12
  public static function getInfo() {
13
    return array(
14
      'name' => 'Field: Boolean',
15
      'description' => 'Test the core views_handler_field_boolean handler.',
16
      'group' => 'Views Handlers',
17
    );
18
  }
19

    
20
  function dataSet() {
21
    // Use default dataset but remove the age from john and paul.
22
    $data = parent::dataSet();
23
    $data[0]['age'] = 0;
24
    $data[3]['age'] = 0;
25
    return $data;
26
  }
27

    
28
  function viewsData() {
29
    $data = parent::viewsData();
30
    $data['views_test']['age']['field']['handler'] = 'views_handler_field_boolean';
31
    return $data;
32
  }
33

    
34
  public function testFieldBoolean() {
35
    $view = $this->getBasicView();
36

    
37
    $view->display['default']->handler->override_option('fields', array(
38
      'age' => array(
39
        'id' => 'age',
40
        'table' => 'views_test',
41
        'field' => 'age',
42
        'relationship' => 'none',
43
      ),
44
    ));
45

    
46
    $this->executeView($view);
47

    
48
    // This is john, which has no age, there are no custom formats defined, yet.
49
    $this->assertEqual(t('No'), $view->field['age']->advanced_render($view->result[0]));
50
    $this->assertEqual(t('Yes'), $view->field['age']->advanced_render($view->result[1]));
51

    
52
    // Reverse the output.
53
    $view->field['age']->options['not'] = TRUE;
54
    $this->assertEqual(t('Yes'), $view->field['age']->advanced_render($view->result[0]));
55
    $this->assertEqual(t('No'), $view->field['age']->advanced_render($view->result[1]));
56

    
57
    unset($view->field['age']->options['not']);
58

    
59
    // Use another output format.
60
    $view->field['age']->options['type'] = 'true-false';
61
    $this->assertEqual(t('False'), $view->field['age']->advanced_render($view->result[0]));
62
    $this->assertEqual(t('True'), $view->field['age']->advanced_render($view->result[1]));
63

    
64
    // test awesome unicode.
65
    $view->field['age']->options['type'] = 'unicode-yes-no';
66
    $this->assertEqual('✖', $view->field['age']->advanced_render($view->result[0]));
67
    $this->assertEqual('✔', $view->field['age']->advanced_render($view->result[1]));
68

    
69
    // Set a custom output format programmatically.
70
    $view->field['age']->formats['test'] = array(t('Test-True'), t('Test-False'));
71
    $view->field['age']->options['type'] = 'test';
72
    $this->assertEqual(t('Test-False'), $view->field['age']->advanced_render($view->result[0]));
73
    $this->assertEqual(t('Test-True'), $view->field['age']->advanced_render($view->result[1]));
74

    
75
    // Set a custom output format through the UI using plain-text inputs.
76
    $view->field['age']->options['type'] = 'custom';
77
    $values = array(
78
      'false' => 'Nay',
79
      'true' => 'Yay',
80
    );
81
    $view->field['age']->options['type_custom_false'] = $values['false'];
82
    $view->field['age']->options['type_custom_true'] = $values['true'];
83
    $this->assertEqual($values['false'], $view->field['age']->advanced_render($view->result[0]));
84
    $this->assertEqual($values['true'], $view->field['age']->advanced_render($view->result[1]));
85

    
86
    // Set a custom output format through the UI using valid HTML inputs.
87
    $view->field['age']->options['type'] = 'custom';
88
    $values = array(
89
      'false' => '<div class="bar">Nay</div>',
90
      'true' => '<div class="foo">Yay</div>',
91
    );
92
    $view->field['age']->options['type_custom_false'] = $values['false'];
93
    $view->field['age']->options['type_custom_true'] = $values['true'];
94
    $this->assertEqual($values['false'], $view->field['age']->advanced_render($view->result[0]));
95
    $this->assertEqual($values['true'], $view->field['age']->advanced_render($view->result[1]));
96

    
97
    // Set a custom output format through the UI using unsafe inputs.
98
    $view->field['age']->options['type'] = 'custom';
99
    $values = array(
100
      'false' => '<script>alert("Nay");</script>',
101
      'true' => '<script>alert("Yay");</script>',
102
    );
103
    $view->field['age']->options['type_custom_false'] = $values['false'];
104
    $view->field['age']->options['type_custom_true'] = $values['true'];
105
    $this->assertNotEqual($values['false'], $view->field['age']->advanced_render($view->result[0]));
106
    $this->assertNotEqual($values['true'], $view->field['age']->advanced_render($view->result[1]));
107
  }
108

    
109
}