1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TractorCow\Fluent\Tests\Extension; |
4
|
|
|
|
5
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use SilverStripe\ORM\DataObject; |
8
|
|
|
use SilverStripe\ORM\Queries\SQLSelect; |
9
|
|
|
use TractorCow\Fluent\Extension\FluentSiteTreeExtension; |
10
|
|
|
use TractorCow\Fluent\State\FluentState; |
11
|
|
|
use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedAnother; |
12
|
|
|
use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedChild; |
13
|
|
|
use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedParent; |
14
|
|
|
use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\UnlocalisedChild; |
15
|
|
|
use TractorCow\Fluent\Tests\Extension\Stub\FluentStubObject; |
16
|
|
|
|
17
|
|
|
class FluentExtensionTest extends SapphireTest |
18
|
|
|
{ |
19
|
|
|
protected static $fixture_file = 'FluentExtensionTest.yml'; |
20
|
|
|
|
21
|
|
|
protected static $extra_dataobjects = [ |
22
|
|
|
LocalisedAnother::class, |
23
|
|
|
LocalisedChild::class, |
24
|
|
|
LocalisedParent::class, |
25
|
|
|
UnlocalisedChild::class, |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
protected static $required_extensions = [ |
29
|
|
|
SiteTree::class => [ |
30
|
|
|
FluentSiteTreeExtension::class, |
31
|
|
|
], |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
public function testFluentLocaleAndFrontendAreAddedToDataQuery() |
35
|
|
|
{ |
36
|
|
|
FluentState::singleton() |
37
|
|
|
->setLocale('test') |
38
|
|
|
->setIsFrontend(true); |
39
|
|
|
|
40
|
|
|
$query = SiteTree::get()->dataQuery(); |
41
|
|
|
$this->assertSame('test', $query->getQueryParam('Fluent.Locale')); |
42
|
|
|
$this->assertTrue($query->getQueryParam('Fluent.IsFrontend')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testGetLocalisedTable() |
46
|
|
|
{ |
47
|
|
|
/** @var SiteTree|FluentSiteTreeExtension $page */ |
48
|
|
|
$page = new SiteTree; |
49
|
|
|
$this->assertSame('SiteTree_Localised', $page->getLocalisedTable('SiteTree')); |
50
|
|
|
$this->assertSame( |
51
|
|
|
'SiteTree_Localised_FR', |
52
|
|
|
$page->getLocalisedTable('SiteTree', 'FR'), |
53
|
|
|
'Table aliases can be generated with getLocalisedTable()' |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testGetLinkingMode() |
58
|
|
|
{ |
59
|
|
|
// Does not have a canViewInLocale method, locale is not current |
60
|
|
|
$stub = new FluentStubObject(); |
61
|
|
|
$this->assertSame('link', $stub->getLinkingMode('foo')); |
62
|
|
|
|
63
|
|
|
// Does not have a canViewInLocale method, locale is current |
64
|
|
|
FluentState::singleton()->setLocale('foo'); |
65
|
|
|
$this->assertSame('current', $stub->getLinkingMode('foo')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testGetLocalisedFields() |
69
|
|
|
{ |
70
|
|
|
// test data_include / data_exclude |
71
|
|
|
// note: These parent fields should be all accessible from the child records as well |
72
|
|
|
$parent = new LocalisedParent(); |
73
|
|
|
$parentLocalised = [ |
74
|
|
|
'Title' => 'Varchar', |
75
|
|
|
'Details' => 'Varchar(200)', |
76
|
|
|
]; |
77
|
|
|
$this->assertEquals( |
78
|
|
|
$parentLocalised, |
79
|
|
|
$parent->getLocalisedFields() |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
// test field_include / field_exclude |
83
|
|
|
$another = new LocalisedAnother(); |
84
|
|
|
$this->assertEquals( |
85
|
|
|
[ |
86
|
|
|
'Bastion' => 'Varchar', |
87
|
|
|
'Data' => 'Varchar(100)', |
88
|
|
|
], |
89
|
|
|
$another->getLocalisedFields() |
90
|
|
|
); |
91
|
|
|
$this->assertEquals( |
92
|
|
|
$parentLocalised, |
93
|
|
|
$another->getLocalisedFields(LocalisedParent::class) |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
// Test translate directly |
97
|
|
|
$child = new LocalisedChild(); |
98
|
|
|
$this->assertEquals( |
99
|
|
|
[ 'Record' => 'Text' ], |
100
|
|
|
$child->getLocalisedFields() |
101
|
|
|
); |
102
|
|
|
$this->assertEquals( |
103
|
|
|
$parentLocalised, |
104
|
|
|
$child->getLocalisedFields(LocalisedParent::class) |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
// Test 'none' |
108
|
|
|
$unlocalised = new UnlocalisedChild(); |
109
|
|
|
$this->assertEmpty($unlocalised->getLocalisedFields()); |
110
|
|
|
$this->assertEquals( |
111
|
|
|
$parentLocalised, |
112
|
|
|
$unlocalised->getLocalisedFields(LocalisedParent::class) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testWritesToCurrentLocale() |
117
|
|
|
{ |
118
|
|
|
FluentState::singleton()->setLocale('en_US'); |
119
|
|
|
$record = $this->objFromFixture(LocalisedParent::class, 'record_a'); |
120
|
|
|
$this->assertSame( |
121
|
|
|
'en_US', |
122
|
|
|
$this->getLocalisedLocaleFromDb($record), |
123
|
|
|
'Record can be read from default locale' |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
FluentState::singleton()->setLocale('de_DE'); |
127
|
|
|
$record->write(); |
128
|
|
|
|
129
|
|
|
$record2 = $this->objFromFixture(LocalisedParent::class, 'record_a'); |
130
|
|
|
$this->assertSame( |
131
|
|
|
'de_DE', |
132
|
|
|
$this->getLocalisedLocaleFromDb($record2), |
133
|
|
|
'Record Locale is set to current locale' |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get a Locale field value directly from a record's localised database table, skipping the ORM |
139
|
|
|
* |
140
|
|
|
* @param DataObject $record |
141
|
|
|
* @return string|null |
142
|
|
|
*/ |
143
|
|
|
protected function getLocalisedLocaleFromDb(DataObject $record) |
144
|
|
|
{ |
145
|
|
|
$result = SQLSelect::create() |
146
|
|
|
->setFrom($record->config()->get('table_name') . '_Localised') |
147
|
|
|
->setWhere(['RecordID' => $record->ID]) |
148
|
|
|
->execute() |
149
|
|
|
->first(); |
150
|
|
|
|
151
|
|
|
return isset($result['Locale']) ? $result['Locale'] : null; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|