Completed
Pull Request — 3.5 (#6973)
by Daniel
17:22
created

GridFieldExportButtonTest::testArrayListInput()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 35
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 28
nc 2
nop 0
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @package framework
5
 * @subpackage tests
6
 */
7
class GridFieldExportButtonTest extends SapphireTest {
8
9
	protected $list;
10
11
	protected $gridField;
12
13
	protected $form;
14
15
	protected static $fixture_file = 'GridFieldExportButtonTest.yml';
16
17
	protected $extraDataObjects = array(
18
		'GridFieldExportButtonTest_Team',
19
		'GridFieldExportButtonTest_NoView'
20
	);
21
22
	public function setUp() {
23
		parent::setUp();
24
25
		$this->list = new DataList('GridFieldExportButtonTest_Team');
26
		$this->list = $this->list->sort('Name');
27
		$config = GridFieldConfig::create()->addComponent(new GridFieldExportButton());
28
		$this->gridField = new GridField('testfield', 'testfield', $this->list, $config);
29
	}
30
31
	public function testCanView() {
32
		$list = new DataList('GridFieldExportButtonTest_NoView');
33
34
		$button = new GridFieldExportButton();
35
		$button->setExportColumns(array('Name' => 'My Name'));
36
37
		$config = GridFieldConfig::create()->addComponent(new GridFieldExportButton());
38
		$gridField = new GridField('testfield', 'testfield', $list, $config);
39
40
		$this->assertEquals(
41
			"\"My Name\"\n",
42
			$button->generateExportFileData($gridField)
43
		);
44
	}
45
46
	public function testGenerateFileDataBasicFields() {
47
		$button = new GridFieldExportButton();
48
		$button->setExportColumns(array('Name' => 'My Name'));
49
50
		$this->assertEquals(
51
			'"My Name"'."\n".
52
			'Test'."\n".
53
			'Test2'."\n",
54
			$button->generateExportFileData($this->gridField)
55
		);
56
	}
57
58
	public function testXLSSanitisation() {
59
		// Create risky object
60
		$object = new GridFieldExportButtonTest_Team();
61
		$object->Name = '=SUM(1, 2)';
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<GridFieldExportButtonTest_Team>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
62
		$object->write();
63
64
		// Export
65
		$button = new GridFieldExportButton();
66
		$button->setExportColumns(array('Name' => 'My Name'));
67
68
		$this->assertEquals(
69
			"\"My Name\"\n\"\t=SUM(1, 2)\"\nTest\nTest2\n",
70
			$button->generateExportFileData($this->gridField)
71
		);
72
	}
73
74
	public function testGenerateFileDataAnonymousFunctionField() {
75
		$button = new GridFieldExportButton();
76
		$button->setExportColumns(array(
77
			'Name' => 'Name',
78
			'City' => function($obj) {
79
				return $obj->getValue() . ' city';
80
			}
81
		));
82
83
		$this->assertEquals(
84
			'Name,City'."\n".
85
			'Test,"City city"'."\n".
86
			'Test2,"Quoted ""City"" 2 city"'."\n",
87
			$button->generateExportFileData($this->gridField)
88
		);
89
	}
90
91
	public function testBuiltInFunctionNameCanBeUsedAsHeader() {
92
		$button = new GridFieldExportButton();
93
		$button->setExportColumns(array(
94
			'Name' => 'Name',
95
			'City' => 'strtolower'
96
		));
97
98
		$this->assertEquals(
99
			'Name,strtolower'."\n".
100
			'Test,City'."\n".
101
			'Test2,"Quoted ""City"" 2"'."\n",
102
			$button->generateExportFileData($this->gridField)
103
		);
104
	}
105
106
	public function testNoCsvHeaders() {
107
		$button = new GridFieldExportButton();
108
		$button->setExportColumns(array(
109
			'Name' => 'Name',
110
			'City' => 'City'
111
		));
112
		$button->setCsvHasHeader(false);
113
114
		$this->assertEquals(
115
			'Test,City'."\n".
116
			'Test2,"Quoted ""City"" 2"'."\n",
117
			$button->generateExportFileData($this->gridField)
118
		);
119
	}
120
121
	public function testArrayListInput() {
122
		$button = new GridFieldExportButton();
123
		$this->gridField->getConfig()->addComponent(new GridFieldPaginator());
124
125
		//Create an ArrayList 1 greater the Paginator's default 15 rows
126
		$arrayList = new ArrayList();
127
		for ($i = 1; $i <= 16; $i++) {
128
			$dataobject = new DataObject(
129
				array ( 'ID' => $i )
130
			);
131
			$arrayList->add($dataobject);
132
		}
133
		$this->gridField->setList($arrayList);
134
135
		$this->assertEquals(
136
			"ID\n".
137
			"1\n".
138
			"2\n".
139
			"3\n".
140
			"4\n".
141
			"5\n".
142
			"6\n".
143
			"7\n".
144
			"8\n".
145
			"9\n".
146
			"10\n".
147
			"11\n".
148
			"12\n".
149
			"13\n".
150
			"14\n".
151
			"15\n".
152
			"16\n",
153
			$button->generateExportFileData($this->gridField)
154
		);
155
	}
156
157
	public function testZeroValue() {
158
		$button = new GridFieldExportButton();
159
		$button->setExportColumns(array(
160
			'RugbyTeamNumber' => 'Rugby Team Number'
161
		));
162
163
		$this->assertEquals(
164
			"\"Rugby Team Number\"\n2\n0\n",
165
			$button->generateExportFileData($this->gridField)
166
		);
167
	}
168
}
169
170
/**
171
 * @package framework
172
 * @subpackage tests
173
 */
174
class GridFieldExportButtonTest_Team extends DataObject implements TestOnly {
175
176
	private static $db = array(
177
		'Name' => 'Varchar',
178
		'City' => 'Varchar',
179
		'RugbyTeamNumber' => 'Int'
180
	);
181
182
	public function canView($member = null) {
183
		return true;
184
	}
185
186
}
187
188
/**
189
 * @package framework
190
 * @subpackage tests
191
 */
192
class GridFieldExportButtonTest_NoView extends DataObject implements TestOnly {
193
194
	private static $db = array(
195
		'Name' => 'Varchar',
196
		'City' => 'Varchar',
197
		'RugbyTeamNumber' => 'Int'
198
	);
199
200
	public function canView($member = null) {
201
		return false;
202
	}
203
204
}
205
206