Passed
Push — master ( 231e0e...4b5ea6 )
by Damian
11:22 queued 10s
created

FluentAdminTraitTest::setUpTestModels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 27
rs 9.7333
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\FluentExtensionTest\LocalisedAnother;
14
use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedChild;
15
use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedParent;
16
use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\MixedLocalisedSortObject;
17
use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\UnlocalisedChild;
18
use TractorCow\Fluent\Tests\Extension\FluentAdminTraitTest\AdminHandler;
19
use TractorCow\Fluent\Tests\Extension\FluentAdminTraitTest\GridObjectVersioned;
20
21
class FluentAdminTraitTest extends SapphireTest
22
{
23
    protected static $fixture_file = 'FluentExtensionTest.yml';
24
25
    protected static $extra_dataobjects = [
26
        // Versioned
27
        GridObjectVersioned::class,
28
        // Non-versioned
29
        LocalisedAnother::class,
30
        LocalisedChild::class,
31
        LocalisedParent::class,
32
        MixedLocalisedSortObject::class,
33
        UnlocalisedChild::class,
34
    ];
35
36
    /**
37
     * @var int
38
     */
39
    protected $recordId = 0;
40
41
    /**
42
     * @throws ValidationException
43
     */
44
    protected function setUp()
45
    {
46
        parent::setUp();
47
        $this->setUpTestModels();
48
        $this->reset();
49
    }
50
51
    protected function tearDown()
52
    {
53
        parent::tearDown();
54
        $this->reset();
55
    }
56
57
    /**
58
     * Generate test models during runtime as writing the fixture is way too complicated
59
     *
60
     * @throws ValidationException
61
     */
62
    protected function setUpTestModels(): void
63
    {
64
        FluentState::singleton()->withState(function (FluentState $state): void {
65
            $state->setLocale('en_US');
66
67
            // draft only object
68
            $object = GridObjectVersioned::create();
69
            $object->Title = 'A record';
70
            $object->Description = 'Not very interesting';
71
            $object->write();
72
73
            $this->recordId = (int) $object->ID;
74
        });
75
76
        FluentState::singleton()->withState(function (FluentState $state): void {
77
            $state->setLocale('de_DE');
78
79
            // published object with different stages
80
            /** @var GridObjectVersioned $object */
81
            $object = GridObjectVersioned::get()->byID($this->recordId);
82
            $object->Title = 'Eine Akte';
83
            $object->Description = 'Live live de hahaha';
84
            $object->write();
85
            $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

85
            $object->/** @scrutinizer ignore-call */ 
86
                     publishRecursive();
Loading history...
86
87
            $object->Description = 'Nicht sehr interessant';
88
            $object->write();
89
        });
90
    }
91
92
    protected function reset()
93
    {
94
        Locale::clearCached();
95
        Versioned::set_stage(Versioned::DRAFT);
96
        FluentVersionedExtension::reset();
97
        Versioned::reset();
98
    }
99
100
    // Versioned tests
101
102
    public function testClearFluent()
103
    {
104
        FluentState::singleton()->withState(function (FluentState $state) {
105
            $state->setLocale('en_US');
106
            /** @var GridObjectVersioned $object */
107
            $object = GridObjectVersioned::get()->byID($this->recordId);
108
109
            // In 2 locales before
110
            $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

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

169
                return $object->/** @scrutinizer ignore-call */ isPublished();
Loading history...
170
            });
171
172
            $this->assertTrue($baseRecordPublished);
173
            $this->assertFalse($object->isPublished());
174
175
            /** @var Form $form */
176
            $form = Form::create();
177
            $form->loadDataFrom($object);
178
            $message = AdminHandler::singleton()->unpublishFluent([], $form);
179
            $this->assertEquals("Unpublished 'A record' from all locales.", $message);
180
181
            $this->assertFalse($object->isPublished());
182
            $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

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