FluentAdminTraitTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 269
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 147
dl 0
loc 269
rs 10
c 0
b 0
f 0
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testArchiveFluent() 0 35 1
A testDeleteFluent() 0 23 1
A testClearFluent() 0 22 1
A reset() 0 6 1
A testPublishFluent() 0 21 1
A testUnpublishFluent() 0 26 1
A setUpTestModels() 0 41 1
A testCopyFluent() 0 29 1
A tearDown() 0 4 1
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\ORM\ValidationException;
9
use SilverStripe\Versioned\Versioned;
10
use TractorCow\Fluent\Extension\FluentVersionedExtension;
11
use TractorCow\Fluent\Model\Locale;
12
use TractorCow\Fluent\State\FluentState;
13
use TractorCow\Fluent\Tests\Extension\FluentAdminTraitTest\AdminHandler;
14
use TractorCow\Fluent\Tests\Extension\FluentAdminTraitTest\GridObjectVersioned;
15
use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedParent;
16
17
class FluentAdminTraitTest extends SapphireTest
18
{
19
    protected static $fixture_file = 'FluentAdminTraitTest.yml';
20
21
    protected static $extra_dataobjects = [
22
        // Versioned
23
        GridObjectVersioned::class,
24
        // Non-versioned
25
        LocalisedParent::class,
26
    ];
27
28
    /**
29
     * @var int
30
     */
31
    protected $versionedRecordId = 0;
32
33
    /**
34
     * @var int
35
     */
36
    protected $nonVersionedRecordId = 0;
37
38
    /**
39
     * @throws ValidationException
40
     */
41
    protected function setUp(): void
42
    {
43
        parent::setUp();
44
        $this->setUpTestModels();
45
        $this->reset();
46
    }
47
48
    protected function tearDown(): void
49
    {
50
        parent::tearDown();
51
        $this->reset();
52
    }
53
54
    /**
55
     * Generate test models during runtime as writing the fixture is way too complicated
56
     *
57
     * @throws ValidationException
58
     */
59
    protected function setUpTestModels(): void
60
    {
61
        FluentState::singleton()->withState(function (FluentState $state): void {
62
            $state->setLocale('en_US');
63
64
            // draft only object
65
            $object = GridObjectVersioned::create();
66
            $object->Title = 'A record';
67
            $object->Description = 'Not very interesting';
68
            $object->write();
69
70
            $this->versionedRecordId = (int) $object->ID;
71
72
            // non-versioned object
73
            $object = LocalisedParent::create();
74
            $object->Title = 'A record';
75
            $object->Description = 'Not very interesting';
76
            $object->write();
77
78
            $this->nonVersionedRecordId = (int) $object->ID;
79
        });
80
81
        FluentState::singleton()->withState(function (FluentState $state): void {
82
            $state->setLocale('de_DE');
83
84
            // published object with different stages
85
            /** @var GridObjectVersioned $object */
86
            $object = GridObjectVersioned::get()->byID($this->versionedRecordId);
87
            $object->Title = 'Eine Akte';
88
            $object->Description = 'Live live de hahaha';
89
            $object->write();
90
            $object->publishRecursive();
0 ignored issues
show
Bug introduced by
The method publishRecursive() 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

90
            $object->/** @scrutinizer ignore-call */ 
91
                     publishRecursive();
Loading history...
91
92
            $object->Description = 'Nicht sehr interessant';
93
            $object->write();
94
95
            /** @var LocalisedParent $object */
96
            $object = LocalisedParent::get()->byID($this->versionedRecordId);
97
            $object->Title = 'Eine Akte';
98
            $object->Description = 'Nicht sehr interessant';
99
            $object->write();
100
        });
101
    }
102
103
    protected function reset()
104
    {
105
        Locale::clearCached();
106
        Versioned::set_stage(Versioned::DRAFT);
107
        FluentVersionedExtension::reset();
108
        Versioned::reset();
109
    }
110
111
    // Versioned tests
112
113
    public function testClearFluent()
114
    {
115
        FluentState::singleton()->withState(function (FluentState $state) {
116
            $state->setLocale('en_US');
117
            /** @var GridObjectVersioned $object */
118
            $object = GridObjectVersioned::get()->byID($this->versionedRecordId);
119
120
            // In 2 locales before
121
            $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

121
            $this->assertTrue($object->/** @scrutinizer ignore-call */ existsInLocale('en_US'));
Loading history...
122
            $this->assertTrue($object->existsInLocale('de_DE'));
123
            $this->assertFalse($object->existsInLocale('es_ES'));
124
125
            /** @var Form $form */
126
            $form = Form::create();
127
            $form->loadDataFrom($object);
128
            $message = AdminHandler::singleton()->clearFluent([], $form);
129
            $this->assertEquals('All localisations have been cleared for \'A record\'.', $message);
130
131
            // In 1 locale after (only current)
132
            $this->assertTrue($object->existsInLocale('en_US'));
133
            $this->assertFalse($object->existsInLocale('de_DE'));
134
            $this->assertFalse($object->existsInLocale('es_ES'));
135
        });
136
    }
137
138
    public function testCopyFluent()
139
    {
140
        FluentState::singleton()->withState(function (FluentState $state) {
141
            $state->setLocale('en_US');
142
            /** @var GridObjectVersioned $object */
143
            $object = GridObjectVersioned::get()->byID($this->versionedRecordId);
144
145
            /** @var Form $form */
146
            $form = Form::create();
147
            $form->loadDataFrom($object);
148
            $message = AdminHandler::singleton()->copyFluent([], $form);
149
            $this->assertEquals('Copied \'A record\' to all other locales.', $message);
150
151
            // Check values in each locale now match en_US version
152
            $data = DB::prepared_query(
153
                <<<'SQL'
154
SELECT "Locale", "Description"
155
FROM "FluentTest_GridObjectVersioned_Localised"
156
WHERE "RecordID" = ?
157
ORDER BY "Locale"
158
SQL
159
                ,
160
                [$object->ID]
161
            )->map();
162
            $this->assertEquals([
163
                'de_DE' => 'Not very interesting',
164
                'en_US' => 'Not very interesting',
165
                'es_ES' => 'Not very interesting',
166
            ], $data);
167
        });
168
    }
169
170
    public function testUnpublishFluent()
171
    {
172
        FluentState::singleton()->withState(function (FluentState $state) {
173
            $state->setLocale('en_US');
174
            /** @var GridObjectVersioned $object */
175
            $object = GridObjectVersioned::get()->byID($this->versionedRecordId);
176
177
            $baseRecordPublished = FluentState::singleton()->withState(function (FluentState $state) use ($object): bool {
178
                $state->setLocale(null);
179
180
                return $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

180
                return $object->/** @scrutinizer ignore-call */ isPublished();
Loading history...
181
            });
182
183
            $this->assertTrue($baseRecordPublished);
184
            $this->assertFalse($object->isPublished());
185
186
            /** @var Form $form */
187
            $form = Form::create();
188
            $form->loadDataFrom($object);
189
            $message = AdminHandler::singleton()->unpublishFluent([], $form);
190
            $this->assertEquals("Unpublished 'A record' from all locales.", $message);
191
192
            $this->assertFalse($object->isPublished());
193
            $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

193
            $this->assertFalse($object->/** @scrutinizer ignore-call */ isPublishedInLocale('de_DE'));
Loading history...
194
            $this->assertFalse($object->isPublishedInLocale('en_US'));
195
            $this->assertFalse($object->isPublishedInLocale('es_ES'));
196
        });
197
    }
198
199
    public function testArchiveFluent()
200
    {
201
        FluentState::singleton()->withState(function (FluentState $state) {
202
            $state->setLocale('en_US');
203
            /** @var GridObjectVersioned $object */
204
            $object = GridObjectVersioned::get()->byID($this->versionedRecordId);
205
            $objectID = $object->ID;
206
207
            /** @var Form $form */
208
            $form = Form::create();
209
            $form->loadDataFrom($object);
210
            $message = AdminHandler::singleton()->archiveFluent([], $form);
211
            $this->assertEquals("Archived 'A record' and all of its localisations.", $message);
212
213
            // Empty tables
214
            $localisations = DB::prepared_query(
215
                'SELECT COUNT(*) FROM "FluentTest_GridObjectVersioned_Localised" WHERE "RecordID" = ?',
216
                [$objectID]
217
            )->value();
218
            $this->assertEquals(0, $localisations);
219
            $liveLocalisations = DB::prepared_query(
220
                'SELECT COUNT(*) FROM "FluentTest_GridObjectVersioned_Localised_Live" WHERE "RecordID" = ?',
221
                [$objectID]
222
            )->value();
223
            $this->assertEquals(0, $liveLocalisations);
224
            $published = DB::prepared_query(
225
                'SELECT COUNT(*) FROM "FluentTest_GridObjectVersioned_Live" WHERE "ID" = ?',
226
                [$objectID]
227
            )->value();
228
            $this->assertEquals(0, $published);
229
            $records = DB::prepared_query(
230
                'SELECT COUNT(*) FROM "FluentTest_GridObjectVersioned" WHERE "ID" = ?',
231
                [$objectID]
232
            )->value();
233
            $this->assertEquals(0, $records);
234
        });
235
    }
236
237
    public function testPublishFluent()
238
    {
239
        FluentState::singleton()->withState(function (FluentState $state) {
240
            $state->setLocale('en_US');
241
            /** @var GridObjectVersioned $object */
242
            $object = GridObjectVersioned::get()->byID($this->versionedRecordId);
243
244
            $this->assertTrue($object->isPublishedInLocale('de_DE'));
245
            $this->assertFalse($object->isPublishedInLocale('en_US'));
246
            $this->assertFalse($object->isPublishedInLocale('es_ES'));
247
248
            /** @var Form $form */
249
            $form = Form::create();
250
            $form->loadDataFrom($object);
251
            $message = AdminHandler::singleton()->publishFluent([], $form);
252
            $this->assertEquals("Published 'A record' across all locales.", $message);
253
254
            $this->assertTrue($object->isPublished());
255
            $this->assertTrue($object->isPublishedInLocale('de_DE'));
256
            $this->assertTrue($object->isPublishedInLocale('en_US'));
257
            $this->assertTrue($object->isPublishedInLocale('es_ES'));
258
        });
259
    }
260
261
    // Unversioned tests
262
263
    public function testDeleteFluent()
264
    {
265
        FluentState::singleton()->withState(function (FluentState $state) {
266
            $state->setLocale('en_US');
267
            /** @var LocalisedParent $object */
268
            $object = LocalisedParent::get()->byID($this->nonVersionedRecordId);
269
270
            $form = Form::create();
271
            $form->loadDataFrom($object);
272
            $message = AdminHandler::singleton()->deleteFluent([], $form);
273
            $this->assertEquals("Deleted 'A record' and all of its localisations.", $message);
274
275
            // Empty tables
276
            $localisations = DB::prepared_query(
277
                'SELECT COUNT(*) FROM "FluentExtensionTest_LocalisedParent_Localised" WHERE "RecordID" = ?',
278
                [$object->ID]
279
            )->value();
280
            $this->assertEquals(0, $localisations);
281
            $records = DB::prepared_query(
282
                'SELECT COUNT(*) FROM "FluentExtensionTest_LocalisedParent" WHERE "ID" = ?',
283
                [$object->ID]
284
            )->value();
285
            $this->assertEquals(0, $records);
286
        });
287
    }
288
}
289