DiffTransformationTest::testTransform()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 3
b 0
f 0
nc 2
nop 0
dl 0
loc 17
rs 9.8666
1
<?php
2
3
namespace SilverStripe\VersionedAdmin\Tests\Forms;
4
5
use InvalidArgumentException;
6
use LogicException;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Dev\SapphireTest;
9
use SilverStripe\Forms\CompositeField;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Forms\Form;
12
use SilverStripe\Forms\TextField;
13
use SilverStripe\ORM\DataObject;
14
use SilverStripe\VersionedAdmin\Forms\DiffTransformation;
15
use SilverStripe\View\ArrayData;
16
17
class DiffTransformationTest extends SapphireTest
18
{
19
    private $testData = [
20
        'First' => 'One',
21
        'Second' => 'Two',
22
        'Third' => 'Three',
23
    ];
24
25
    /**
26
     * @var Form
27
     */
28
    private $testForm;
29
30
    protected function setUp(): void
31
    {
32
        parent::setUp();
33
34
        $fields = FieldList::create();
35
        foreach ($this->testData as $fieldName => $fieldValue) {
36
            $fields->push(TextField::create($fieldName)->setValue($fieldValue));
37
        }
38
39
        $this->testForm = Form::create(
40
            Controller::create(),
41
            'TestForm',
42
            $fields,
43
            FieldList::create()
44
        );
45
46
        // Don't go injecting an extra field to the $fields FieldList
47
        $this->testForm->disableSecurityToken();
48
    }
49
50
    public function testTransform()
51
    {
52
        $form = $this->testForm;
53
        $oldData = [
54
            'First' => '1st',
55
            'Second' => '2nd',
56
            'Third' => '3rd',
57
        ];
58
59
        $expected = $this->getExpected($oldData);
60
        $transformation = DiffTransformation::create();
61
        $form->transform($transformation);
62
        $form->loadDataFrom($oldData);
63
64
        foreach ($form->Fields() as $index => $field) {
65
            $this->assertStringContainsString($expected[$index]['before'], $field->Value(), 'Value before is shown');
66
            $this->assertStringContainsString($expected[$index]['after'], $field->Value(), 'Value after is shown');
67
        }
68
    }
69
70
    public function testTransformWithCompositeFields()
71
    {
72
        $form = $this->testForm;
73
        $form->setFields(
74
            FieldList::create(
75
                CompositeField::create($form->Fields())
76
            )
77
        );
78
        $oldData = [
79
            'First' => 'Uno',
80
            'Second' => 'Dos',
81
            'Third' => 'Tres',
82
        ];
83
84
        $expected = $this->getExpected($oldData);
85
        $transformation = DiffTransformation::create();
86
        $form->transform($transformation);
87
        $form->loadDataFrom($oldData);
88
89
        foreach (array_values($form->Fields()->dataFields()) as $index => $field) {
90
            $this->assertStringContainsString($expected[$index]['before'], $field->Value(), 'Value before is shown');
91
            $this->assertStringContainsString($expected[$index]['after'], $field->Value(), 'Value after is shown');
92
        }
93
    }
94
95
    /**
96
     * Helper method for generating the expected result for diff views between fields
97
     *
98
     * @param array $outdated
99
     * @return array
100
     */
101
    private function getExpected($outdated)
102
    {
103
        $expected = [];
104
        $current = $this->testData;
105
        foreach (array_combine(array_values($outdated), array_values($current)) as $now => $was) {
106
            $expected[] = [
107
                'before' => "<ins>$was</ins>",
108
                'after' => "<del>$now</del>",
109
            ];
110
        }
111
        return $expected;
112
    }
113
}
114