1 | <?php |
||
5 | class GridTest extends GridBuilderTestCase |
||
6 | { |
||
7 | public function testAddColumn() |
||
8 | { |
||
9 | $grid = new Grid($this->gridHelper); |
||
10 | $grid->addColumn('name', [ |
||
11 | 'title' => 'First name' |
||
12 | ]); |
||
13 | $grid->addColumn('sex', [ |
||
14 | 'title' => 'Sex', |
||
15 | 'sortable' => true |
||
16 | ]); |
||
17 | |||
18 | $this->assertInstanceOf(\Boduch\Grid\Column::class, $grid->getColumns()['name']); |
||
19 | $this->assertEquals('First name', $grid->getColumns()['name']->getTitle()); |
||
20 | |||
21 | $this->assertInstanceOf(\Boduch\Grid\Column::class, $grid->getColumns()['sex']); |
||
22 | $this->assertEquals('Sex', $grid->getColumns()['sex']->getTitle()); |
||
23 | $this->assertTrue($grid->getColumns()['sex']->isSortable()); |
||
24 | } |
||
25 | |||
26 | public function testAddColumnWithDecorators() |
||
27 | { |
||
28 | $grid = new Grid($this->gridHelper); |
||
29 | $grid->addColumn('name', [ |
||
30 | 'title' => 'First name', |
||
31 | 'clickable' => function () { |
||
32 | return ''; |
||
33 | }, |
||
34 | 'decorators' => [ |
||
35 | new \Boduch\Grid\Decorators\Url() |
||
36 | ] |
||
37 | ]); |
||
38 | |||
39 | $column = $grid->getColumns()['name']; |
||
40 | |||
41 | $this->assertEquals(2, count($column->getDecorators())); |
||
42 | $this->assertInstanceOf(\Boduch\Grid\Decorators\DecoratorInterface::class, $column->getDecorators()[0]); |
||
43 | $this->assertInstanceOf(\Boduch\Grid\Decorators\DecoratorInterface::class, $column->getDecorators()[1]); |
||
44 | } |
||
45 | |||
46 | public function testRenderColumnWithDecorator() |
||
47 | { |
||
48 | $grid = $this->getSampleGrid(); |
||
49 | $rows = $grid->getRows(); |
||
50 | |||
51 | $this->assertInstanceOf(\Boduch\Grid\Rows::class, $rows); |
||
52 | $this->assertInstanceOf(\Boduch\Grid\Row::class, $rows[0]); |
||
53 | |||
54 | $this->assertEquals("<a href=\"http://4programmers.net\">\nhttp://4programmers.net\n</a>\n", (string) $rows[0]->getValue('website')); |
||
55 | $this->assertEquals("<a href=\"http://4programmers.net\">1</a>", (string) $rows[0]->getValue('id')); |
||
56 | } |
||
57 | |||
58 | public function testBuildGridWithEachCallbackAndModifyColumnValue() |
||
59 | { |
||
60 | $grid = $this->getSampleGrid(); |
||
61 | $grid->after(function (\Boduch\Grid\Row $row) { |
||
62 | $row->get('website')->setValue(''); |
||
63 | }); |
||
64 | |||
65 | $rows = $grid->getRows(); |
||
66 | |||
67 | $this->assertEquals('', (string) $rows[0]->getValue('website')); |
||
68 | } |
||
69 | |||
70 | public function testBuildGridAndAddRowClass() |
||
71 | { |
||
72 | $grid = $this->getSampleGrid(); |
||
73 | $grid->after(function (\Boduch\Grid\Row $row) { |
||
74 | $row->class = 'foo'; |
||
75 | }); |
||
76 | |||
77 | $rows = $grid->getRows(); |
||
78 | |||
79 | $this->assertEquals('foo', (string) $rows[0]->class); |
||
80 | } |
||
81 | |||
82 | public function tesAutoescapeCellOutput() |
||
83 | { |
||
84 | $grid = new Grid($this->gridHelper); |
||
85 | $grid->addColumn('xss', [ |
||
86 | 'title' => 'xss' |
||
87 | ]); |
||
88 | |||
89 | $collection = collect([ |
||
90 | ['xss' => '<xss>'] |
||
91 | ]); |
||
92 | |||
93 | $source = new \Boduch\Grid\Source\CollectionSource($collection); |
||
94 | $grid->setSource($source); |
||
95 | |||
96 | $rows = $grid->getRows(); |
||
97 | |||
98 | $this->assertEquals('<xss>', (string) $rows[0]->xss); |
||
99 | } |
||
100 | |||
101 | private function getSampleGrid() |
||
102 | { |
||
103 | $grid = new Grid($this->gridHelper); |
||
104 | $grid->addColumn('website', [ |
||
105 | 'title' => 'Website', |
||
106 | 'decorators' => [ |
||
107 | new \Boduch\Grid\Decorators\Url() |
||
108 | ] |
||
127 |