Source of file FluentExtensionTest.php
Size: 12,494 Bytes - Last Modified: 2021-12-23T10:53:31+00:00
/var/www/docs.ssmods.com/process/src/tests/php/Extension/FluentExtensionTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 | <?php namespace TractorCow\Fluent\Tests\Extension; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Dev\SapphireTest; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\Queries\SQLSelect; use TractorCow\Fluent\Extension\FluentSiteTreeExtension; use TractorCow\Fluent\Model\Locale; use TractorCow\Fluent\State\FluentState; use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedAnother; use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedChild; use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedParent; use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\MixedLocalisedSortObject; use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\UnlocalisedChild; use TractorCow\Fluent\Tests\Extension\Stub\FluentStubObject; class FluentExtensionTest extends SapphireTest { protected static $fixture_file = 'FluentExtensionTest.yml'; protected static $extra_dataobjects = [ LocalisedAnother::class, LocalisedChild::class, LocalisedParent::class, MixedLocalisedSortObject::class, UnlocalisedChild::class, ]; protected static $required_extensions = [ SiteTree::class => [ FluentSiteTreeExtension::class, ], ]; protected function setUp() { parent::setUp(); Locale::clearCached(); } public function testFluentLocaleAndFrontendAreAddedToDataQuery() { FluentState::singleton()->withState(function (FluentState $newState) { $newState ->setLocale('test') ->setIsFrontend(true); $query = SiteTree::get()->dataQuery(); $this->assertSame('test', $query->getQueryParam('Fluent.Locale')); $this->assertTrue($query->getQueryParam('Fluent.IsFrontend')); }); } public function testGetLocalisedTable() { /** @var SiteTree|FluentSiteTreeExtension $page */ $page = new SiteTree; $this->assertSame('SiteTree_Localised', $page->getLocalisedTable('SiteTree')); $this->assertSame( 'SiteTree_Localised_FR', $page->getLocalisedTable('SiteTree', 'FR'), 'Table aliases can be generated with getLocalisedTable()' ); } public function testGetLinkingMode() { // Does not have a canViewInLocale method, locale is not current FluentState::singleton()->withState(function (FluentState $newState) { $newState->setLocale('en_US'); $stub = new FluentStubObject(); $this->assertSame('current', $stub->LocaleInformation('en_US')->getLinkingMode()); $this->assertSame('link', $stub->LocaleInformation('de_DE')->getLinkingMode()); $newState->setLocale('de_DE'); $this->assertSame('link', $stub->LocaleInformation('en_US')->getLinkingMode()); $this->assertSame('current', $stub->LocaleInformation('de_DE')->getLinkingMode()); }); } public function testGetLocalisedFields() { // test data_include / data_exclude // note: These parent fields should be all accessible from the child records as well $parent = new LocalisedParent(); $parentLocalised = [ 'Title' => 'Varchar', 'Details' => 'Varchar(200)', ]; $this->assertEquals( $parentLocalised, $parent->getLocalisedFields() ); // test field_include / field_exclude $another = new LocalisedAnother(); $this->assertEquals( [ 'Bastion' => 'Varchar', 'Data' => 'Varchar(100)', ], $another->getLocalisedFields() ); $this->assertEquals( $parentLocalised, $another->getLocalisedFields(LocalisedParent::class) ); // Test translate directly $child = new LocalisedChild(); $this->assertEquals( [ 'Record' => 'Text' ], $child->getLocalisedFields() ); $this->assertEquals( $parentLocalised, $child->getLocalisedFields(LocalisedParent::class) ); // Test 'none' $unlocalised = new UnlocalisedChild(); $this->assertEmpty($unlocalised->getLocalisedFields()); $this->assertEquals( $parentLocalised, $unlocalised->getLocalisedFields(LocalisedParent::class) ); } public function testWritesToCurrentLocale() { FluentState::singleton()->withState(function (FluentState $newState) { $newState->setLocale('en_US'); $record = $this->objFromFixture(LocalisedParent::class, 'record_a'); $this->assertTrue( $this->hasLocalisedRecord($record, 'en_US'), 'Record can be read from default locale' ); }); FluentState::singleton()->withState(function (FluentState $newState) { $newState->setLocale('de_DE'); $record2 = $this->objFromFixture(LocalisedParent::class, 'record_a'); $this->assertTrue( $this->hasLocalisedRecord($record2, 'de_DE'), 'Existing record can be read from German locale' ); $newState->setLocale('es_ES'); $record2->Title = 'Un archivo'; $record2->write(); $record3 = $this->objFromFixture(LocalisedParent::class, 'record_a'); $this->assertTrue( $this->hasLocalisedRecord($record3, 'es_ES'), 'Record Locale is set to current locale when writing new records' ); }); } public function testLocalisedMixSorting() { FluentState::singleton()->withState(function (FluentState $newState) { $newState->setLocale('en_US'); // Sort by the NonLocalisedSort field first then the LocalisedField second both in ascending order // so the result will be opposite if the order of the columns is not maintained $objects=MixedLocalisedSortObject::get()->sort( '"FluentExtensionTest_MixedLocalisedSortObject"."LocalizedSort", '. '"FluentExtensionTest_MixedLocalisedSortObject"."NonLocalizedSort", '. '"FluentExtensionTest_MixedLocalisedSortObject"."Title"' ); // Make sure Item A is first $this->assertEquals( 'Item A', $objects->offsetGet(0)->Title ); // Make sure Item B is second $this->assertEquals( 'Item B', $objects->offsetGet(1)->Title ); // Make sure Item C is third $this->assertEquals( 'Item C', $objects->offsetGet(2)->Title ); }); } /** * Ensure that records can be sorted in their locales * * @dataProvider sortRecordProvider * @param string $locale * @param string[] $sortArgs * @param string[] $expected * @group exclude-from-travis */ public function testLocalisedFieldsCanBeSorted($locale, array $sortArgs, $expected) { FluentState::singleton()->withState(function (FluentState $newState) use ($locale, $sortArgs, $expected) { $newState->setLocale($locale); $records = LocalisedParent::get()->sort(...$sortArgs); $titles = $records->column('Title'); $this->assertEquals($expected, $titles); }); } /** * @return array[] Keys: Locale, sorting arguments, expected titles in result */ public function sortRecordProvider() { return [ /** * Single field (non-composite) sorting syntax (either string or array syntax) * * E.g. `->sort('"foo"')`, `->sort('Title', 'DESC')` etc */ 'german ascending single sort' => [ 'de_DE', ['Title', 'ASC'], ['Eine Akte', 'Lesen Sie mehr', 'Rennen'], ], 'german descending single sort' => [ 'de_DE', ['"Title" DESC'], ['Rennen', 'Lesen Sie mehr', 'Eine Akte'], ], 'english ascending single sort' => [ 'en_US', ['"Title" ASC'], ['A record', 'Go for a run', 'Read about things'], ], 'english descending single sort' => [ 'en_US', ['Title', 'DESC'], ['Read about things', 'Go for a run', 'A record'], ], 'english ascending on unlocalised field' => [ 'en_US', ['"Description"'], ['Read about things', 'Go for a run', 'A record'], ], 'english descending on unlocalised field' => [ 'en_US', ['"Description" DESC'], ['A record', 'Read about things', 'Go for a run'], ], 'german ascending on unlocalised field' => [ 'de_DE', ['"Description"'], ['Lesen Sie mehr', 'Rennen', 'Eine Akte'], ], 'german descending on unlocalised field' => [ 'de_DE', ['"Description" DESC'], ['Eine Akte', 'Lesen Sie mehr', 'Rennen'], ], /** * Composite sorting tests (either string syntax or array syntax) * * E.g. `->sort(['foo' => 'ASC', 'bar' => 'DESC'])` */ 'english composite sort, string' => [ 'en_US', ['"Details" ASC, "Title" ASC'], ['Go for a run', 'A record', 'Read about things'] ], 'german composite sort, string' => [ 'de_DE', ['"Details" ASC, "Title" ASC'], ['Rennen', 'Eine Akte', 'Lesen Sie mehr'], ], 'english, composite sort, array' => [ 'en_US', [[ 'Details' => 'ASC', 'Title' => 'ASC' ]], ['Go for a run', 'A record', 'Read about things'], ], 'german, composite sort, array' => [ 'de_DE', [[ 'Details' => 'ASC', 'Title' => 'ASC' ]], ['Rennen', 'Eine Akte', 'Lesen Sie mehr'], ], 'german, composite sort, array (flipped)' => [ 'de_DE', [[ 'Details' => 'ASC', 'Title' => 'DESC' ]], ['Rennen', 'Lesen Sie mehr', 'Eine Akte'], ], 'english, composite sort, array (flipped)' => [ 'en_US', [[ 'Details' => 'DESC', 'Title' => 'DESC' ]], ['Read about things', 'A record', 'Go for a run'], ], 'german, composite sort, no directions' => [ 'de_DE', ['"Details", "Title"'], ['Rennen', 'Eine Akte', 'Lesen Sie mehr'], ], /** * Ignored types of sorting, e.g. subqueries. Ignored sorting should use the ORM default * and sort on whatever is in the base table. */ 'english, subquery sort' => [ 'en_US', ['CONCAT((SELECT COUNT(*) FROM "FluentExtensionTest_LocalisedParent_Localised"), "FluentExtensionTest_LocalisedParent"."ID")'], ['A record', 'Read about things', 'Go for a run'], ] ]; } /** * Get a Locale field value directly from a record's localised database table, skipping the ORM * * @param DataObject $record * @param string $locale * @return boolean */ protected function hasLocalisedRecord(DataObject $record, $locale) { $result = SQLSelect::create() ->setFrom('"' . $record->config()->get('table_name') . '_Localised"') ->setWhere([ '"RecordID"' => $record->ID, '"Locale"' => $locale, ]) ->execute() ->first(); return !empty($result); } } |