Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
MNT Fix broken unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Jan 26, 2023
1 parent 759c49e commit 5ab1826
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
15 changes: 10 additions & 5 deletions tests/php/Extension/FluentExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function testLocalisedMixSorting()

// 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(
$objects=MixedLocalisedSortObject::get()->orderBy(
'"FluentExtensionTest_MixedLocalisedSortObject"."LocalizedSort", '.
'"FluentExtensionTest_MixedLocalisedSortObject"."NonLocalizedSort", '.
'"FluentExtensionTest_MixedLocalisedSortObject"."Title"'
Expand Down Expand Up @@ -289,12 +289,16 @@ public function testLocalisedCopyClassNameChange(): void
* @param string[] $expected
* @group exclude-from-travis
*/
public function testLocalisedFieldsCanBeSorted($locale, array $sortArgs, $expected)
public function testLocalisedFieldsCanBeSorted($locale, array $sortArgs, $expected, $useOrderBy = false)
{
FluentState::singleton()->withState(function (FluentState $newState) use ($locale, $sortArgs, $expected) {
FluentState::singleton()->withState(function (FluentState $newState) use ($locale, $sortArgs, $expected, $useOrderBy) {
$newState->setLocale($locale);

$records = LocalisedParent::get()->sort(...$sortArgs);
if ($useOrderBy) {
$records = LocalisedParent::get()->orderBy(...$sortArgs);
} else {
$records = LocalisedParent::get()->sort(...$sortArgs);
}
$titles = $records->column('Title');
$this->assertEquals($expected, $titles);
});
Expand Down Expand Up @@ -411,6 +415,7 @@ public function sortRecordProvider()
'en_US',
['CONCAT((SELECT COUNT(*) FROM "FluentExtensionTest_LocalisedParent_Localised"), "FluentExtensionTest_LocalisedParent"."ID")'],
['A record', 'Read about things', 'Go for a run'],
true,
]
];
}
Expand All @@ -431,7 +436,7 @@ protected function hasLocalisedRecord(DataObject $record, $locale)
'"Locale"' => $locale,
])
->execute()
->first();
->record();

return !empty($result);
}
Expand Down
7 changes: 4 additions & 3 deletions tests/php/Extension/FluentSiteTreeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Page;
use SilverStripe\CMS\Forms\SiteTreeURLSegmentField;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
Expand Down Expand Up @@ -74,8 +75,8 @@ public function testGetLocaleInformation()
$this->assertEquals('English (New Zealand)', $result->getTitle());
$this->assertEquals('English', $result->getLanguageNative());
$this->assertEquals('en', $result->getLanguage());
$this->assertEquals('/newzealand/a-page/', $result->getLink());
$this->assertEquals('http://mocked/newzealand/a-page/', $result->getAbsoluteLink());
$this->assertEquals(Controller::normaliseTrailingSlash('/newzealand/a-page/'), $result->getLink());
$this->assertEquals(Controller::normaliseTrailingSlash('http://mocked/newzealand/a-page/'), $result->getAbsoluteLink());
$this->assertEquals('link', $result->getLinkingMode());
$this->assertEquals('newzealand', $result->getURLSegment());
});
Expand Down Expand Up @@ -179,7 +180,7 @@ function (FluentState $newState) use ($domain, $locale, $prefixDisabled, $pageNa

/** @var Page|FluentSiteTreeExtension $page */
$page = $this->objFromFixture(Page::class, $pageName);
$this->assertEquals($url, $page->Link());
$this->assertEquals(Controller::normaliseTrailingSlash($url), $page->Link());
}
);
}
Expand Down
13 changes: 7 additions & 6 deletions tests/php/Model/LocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TractorCow\Fluent\Tests\Model;

use SilverStripe\Control\Controller;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\CheckboxField;
Expand Down Expand Up @@ -101,25 +102,25 @@ public function testGetBaseURLContainsDomainAndURLSegmentForNonDefaultLocale()
// es_ES has a domain but is not the default locale for that domain
$result = Locale::getByLocale('es_ES')->getBaseURL();
$this->assertStringContainsString('fluent.es', $result, "Locale's domain is in the URL");
$this->assertStringContainsString('/es/', $result, 'URL segment for non-default locale is in the URL');
$this->assertStringEndsWith(Controller::normaliseTrailingSlash('/es/'), $result, 'URL segment for non-default locale is in the URL');

// Turning off domain mode removes domain but not prefix
FluentState::singleton()->setIsDomainMode(false);
$result = Locale::getByLocale('es_ES')->getBaseURL();
$this->assertStringNotContainsString('fluent.es', $result, "Locale's domain is in the URL");
$this->assertStringContainsString('/es/', $result, 'URL segment for non-default locale is in the URL');
$this->assertStringEndsWith(Controller::normaliseTrailingSlash('/es/'), $result, 'URL segment for non-default locale is in the URL');
}

public function testBaseURLPrefixDisabled()
{
// Default base url includes the default url segment
$result = Locale::getDefault()->getBaseURL();
$this->assertStringContainsString('/au/', $result);
$this->assertStringEndsWith(Controller::normaliseTrailingSlash('/au/'), $result);

// Default base url shortens the default locale url base by excluding the locale's url segment
Config::inst()->set(FluentDirectorExtension::class, 'disable_default_prefix', true);
$result = Locale::getDefault()->getBaseURL();
$this->assertStringNotContainsString('/au/', $result);
$this->assertStringEndsNotWith(Controller::normaliseTrailingSlash('/au/'), $result);
}

public function testGetBaseURLOnlyContainsDomainForPrefixDisabledDefaultLocale()
Expand All @@ -129,13 +130,13 @@ public function testGetBaseURLOnlyContainsDomainForPrefixDisabledDefaultLocale()
// es_US has a domain and is the default
$result = Locale::getByLocale('es_US')->getBaseURL();
$this->assertStringContainsString('fluent.es', $result, "Locale's domain is in the URL");
$this->assertStringNotContainsString('/es-usa/', $result, 'URL segment is not in the URL for default locales');
$this->assertStringEndsNotWith(Controller::normaliseTrailingSlash('/es-usa/'), $result, 'URL segment is not in the URL for default locales');

// When domain mode is turned off, prefix is now necessary
FluentState::singleton()->setIsDomainMode(false);
$result = Locale::getByLocale('es_US')->getBaseURL();
$this->assertStringNotContainsString('fluent.es', $result, "Domain not used");
$this->assertStringContainsString('/es-usa/', $result, 'URL Segment necessary for non-global default');
$this->assertStringEndsWith(Controller::normaliseTrailingSlash('/es-usa/'), $result, 'URL Segment necessary for non-global default');
}

public function testGetSiblings()
Expand Down

0 comments on commit 5ab1826

Please sign in to comment.