Passed
Push — master ( d7466e...1cd753 )
by Damian
02:23
created

FluentAdminTraitTest::testClearFluent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 22
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace TractorCow\Fluent\Tests\Extension;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\Forms\Form;
7
use SilverStripe\ORM\DB;
8
use SilverStripe\Versioned\Versioned;
9
use TractorCow\Fluent\Extension\FluentVersionedExtension;
10
use TractorCow\Fluent\Model\Locale;
11
use TractorCow\Fluent\State\FluentState;
12
use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedAnother;
13
use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedChild;
14
use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedParent;
15
use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\MixedLocalisedSortObject;
16
use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\UnlocalisedChild;
17
use TractorCow\Fluent\Tests\Extension\FluentAdminTraitTest\AdminHandler;
18
use TractorCow\Fluent\Tests\Extension\FluentAdminTraitTest\GridObjectVersioned;
19
20
class FluentAdminTraitTest extends SapphireTest
21
{
22
    protected static $fixture_file = [
23
        'FluentAdminTraitTest.yml',
24
        'FluentExtensionTest.yml',
25
    ];
26
27
    protected static $extra_dataobjects = [
28
        // Versioned
29
        GridObjectVersioned::class,
30
        // Non-versioned
31
        LocalisedAnother::class,
32
        LocalisedChild::class,
33
        LocalisedParent::class,
34
        MixedLocalisedSortObject::class,
35
        UnlocalisedChild::class,
36
    ];
37
38
    protected function setUp()
39
    {
40
        parent::setUp();
41
        $this->reset();
42
    }
43
44
    protected function tearDown()
45
    {
46
        parent::tearDown();
47
        $this->reset();
48
    }
49
50
    protected function reset()
51
    {
52
        Locale::clearCached();
53
        Versioned::set_stage(Versioned::DRAFT);
54
        FluentVersionedExtension::reset();
55
        Versioned::reset();
56
    }
57
58
    // Versioned tests
59
60
    public function testClearFluent()
61
    {
62
        FluentState::singleton()->withState(function (FluentState $state) {
63
            $state->setLocale('en_US');
64
            /** @var GridObjectVersioned $object */
65
            $object = $this->objFromFixture(GridObjectVersioned::class, 'record_a');
66
67
            // In 2 locales before
68
            $this->assertTrue($object->existsInLocale('en_US'));
0 ignored issues
show
Bug introduced by
The method existsInLocale() does not exist on TractorCow\Fluent\Tests\...est\GridObjectVersioned. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
            $this->assertTrue($object->/** @scrutinizer ignore-call */ existsInLocale('en_US'));
Loading history...
69
            $this->assertTrue($object->existsInLocale('de_DE'));
70
            $this->assertFalse($object->existsInLocale('es_ES'));
71
72
            /** @var Form $form */
73
            $form = Form::create();
74
            $form->loadDataFrom($object);
75
            $message = AdminHandler::singleton()->clearFluent([], $form);
76
            $this->assertEquals('All localisations have been cleared for \'A record\'.', $message);
77
78
            // In 1 locale after (only current)
79
            $this->assertTrue($object->existsInLocale('en_US'));
80
            $this->assertFalse($object->existsInLocale('de_DE'));
81
            $this->assertFalse($object->existsInLocale('es_ES'));
82
        });
83
    }
84
85
    public function testCopyFluent()
86
    {
87
        FluentState::singleton()->withState(function (FluentState $state) {
88
            $state->setLocale('en_US');
89
            /** @var GridObjectVersioned $object */
90
            $object = $this->objFromFixture(GridObjectVersioned::class, 'record_a');
91
92
            /** @var Form $form */
93
            $form = Form::create();
94
            $form->loadDataFrom($object);
95
            $message = AdminHandler::singleton()->copyFluent([], $form);
96
            $this->assertEquals('Copied \'A record\' to all other locales.', $message);
97
98
            // Check values in each locale now match en_US version
99
            $data = DB::prepared_query(
100
                <<<'SQL'
101
SELECT "Locale", "Description"
102
FROM "FluentTest_GridObjectVersioned_Localised"
103
WHERE "RecordID" = ?
104
ORDER BY "Locale"
105
SQL
106
                ,
107
                [$object->ID]
108
            )->map();
109
            $this->assertEquals([
110
                'de_DE' => 'Not very interesting',
111
                'en_US' => 'Not very interesting',
112
                'es_ES' => 'Not very interesting',
113
            ], $data);
114
        });
115
    }
116
117
    public function testUnpublishFluent()
118
    {
119
        FluentState::singleton()->withState(function (FluentState $state) {
120
            $state->setLocale('en_US');
121
            /** @var GridObjectVersioned $object */
122
            $object = $this->objFromFixture(GridObjectVersioned::class, 'record_a');
123
            $this->assertTrue($object->isPublished());
0 ignored issues
show
Bug introduced by
The method isPublished() does not exist on TractorCow\Fluent\Tests\...est\GridObjectVersioned. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

123
            $this->assertTrue($object->/** @scrutinizer ignore-call */ isPublished());
Loading history...
124
125
            /** @var Form $form */
126
            $form = Form::create();
127
            $form->loadDataFrom($object);
128
            $message = AdminHandler::singleton()->unpublishFluent([], $form);
129
            $this->assertEquals("Unpublished 'A record' from all locales.", $message);
130
131
            $this->assertFalse($object->isPublished());
132
            $this->assertFalse($object->isPublishedInLocale('de_DE'));
0 ignored issues
show
Bug introduced by
The method isPublishedInLocale() does not exist on TractorCow\Fluent\Tests\...est\GridObjectVersioned. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
            $this->assertFalse($object->/** @scrutinizer ignore-call */ isPublishedInLocale('de_DE'));
Loading history...
133
            $this->assertFalse($object->isPublishedInLocale('en_US'));
134
            $this->assertFalse($object->isPublishedInLocale('es_ES'));
135
        });
136
    }
137
138
    public function testArchiveFluent()
139
    {
140
        FluentState::singleton()->withState(function (FluentState $state) {
141
            $state->setLocale('en_US');
142
            /** @var GridObjectVersioned $object */
143
            $object = $this->objFromFixture(GridObjectVersioned::class, 'record_a');
144
            $objectID = $object->ID;
145
146
            /** @var Form $form */
147
            $form = Form::create();
148
            $form->loadDataFrom($object);
149
            $message = AdminHandler::singleton()->archiveFluent([], $form);
150
            $this->assertEquals("Archived 'A record' and all of its localisations.", $message);
151
152
            // Empty tables
153
            $localisations = DB::prepared_query(
154
                'SELECT COUNT(*) FROM "FluentTest_GridObjectVersioned_Localised" WHERE "RecordID" = ?',
155
                [$objectID]
156
            )->value();
157
            $this->assertEquals(0, $localisations);
158
            $liveLocalisations = DB::prepared_query(
159
                'SELECT COUNT(*) FROM "FluentTest_GridObjectVersioned_Localised_Live" WHERE "RecordID" = ?',
160
                [$objectID]
161
            )->value();
162
            $this->assertEquals(0, $liveLocalisations);
163
            $published = DB::prepared_query(
164
                'SELECT COUNT(*) FROM "FluentTest_GridObjectVersioned_Live" WHERE "ID" = ?',
165
                [$objectID]
166
            )->value();
167
            $this->assertEquals(0, $published);
168
            $records = DB::prepared_query(
169
                'SELECT COUNT(*) FROM "FluentTest_GridObjectVersioned" WHERE "ID" = ?',
170
                [$objectID]
171
            )->value();
172
            $this->assertEquals(0, $records);
173
        });
174
    }
175
176
    public function testPublishFluent()
177
    {
178
        FluentState::singleton()->withState(function (FluentState $state) {
179
            $state->setLocale('en_US');
180
            /** @var GridObjectVersioned $object */
181
            $object = $this->objFromFixture(GridObjectVersioned::class, 'record_a');
182
183
            $this->assertTrue($object->isPublishedInLocale('de_DE'));
184
            $this->assertFalse($object->isPublishedInLocale('en_US'));
185
            $this->assertFalse($object->isPublishedInLocale('es_ES'));
186
187
            /** @var Form $form */
188
            $form = Form::create();
189
            $form->loadDataFrom($object);
190
            $message = AdminHandler::singleton()->publishFluent([], $form);
191
            $this->assertEquals("Published 'A record' across all locales.", $message);
192
193
            $this->assertTrue($object->isPublished());
194
            $this->assertTrue($object->isPublishedInLocale('de_DE'));
195
            $this->assertTrue($object->isPublishedInLocale('en_US'));
196
            $this->assertTrue($object->isPublishedInLocale('es_ES'));
197
        });
198
    }
199
200
    // Unversioned tests
201
202
    public function testDeleteFluent()
203
    {
204
        FluentState::singleton()->withState(function (FluentState $state) {
205
            $state->setLocale('en_US');
206
            /** @var LocalisedParent $object */
207
            $object = $this->objFromFixture(LocalisedParent::class, 'record_a');
208
209
            /** @var Form $form */
210
            $form = Form::create();
211
            $form->loadDataFrom($object);
212
            $message = AdminHandler::singleton()->deleteFluent([], $form);
213
            $this->assertEquals("Deleted 'A record' and all of its localisations.", $message);
214
215
            // Empty tables
216
            $localisations = DB::prepared_query(
217
                'SELECT COUNT(*) FROM "FluentExtensionTest_LocalisedParent_Localised" WHERE "RecordID" = ?',
218
                [$object->ID]
219
            )->value();
220
            $this->assertEquals(0, $localisations);
221
            $records = DB::prepared_query(
222
                'SELECT COUNT(*) FROM "FluentExtensionTest_LocalisedParent" WHERE "ID" = ?',
223
                [$object->ID]
224
            )->value();
225
            $this->assertEquals(0, $records);
226
        });
227
    }
228
}
229