Commit f4842906 authored by Robbie Averill's avatar Robbie Averill

API Add getSelectedLanguage method to BasePage, rename BasePageTests so they run

parent 31b1cdff
......@@ -250,6 +250,24 @@ class BasePage extends SiteTree {
else return null;
}
/**
* Returns the native language name for the selected locale/language, empty string if Translatable is not available
*
* @return string
*/
public function getSelectedLanguage()
{
if (!class_exists('Translatable')) {
return '';
}
$language = explode('_', Translatable::get_current_locale());
$languageCode = array_shift($language);
$nativeName = i18n::get_language_name($languageCode, true);
return $nativeName;
}
}
class BasePage_Controller extends ContentController {
......
<?php
class BasePageTest extends SapphireTest
{
public static $fixture_file = 'BasePageTest.yml';
public function setUp()
{
parent::setUp();
Config::nest();
Config::inst()->update('BasePage', 'pdf_export', true);
Config::inst()->update('BasePage', 'generated_pdf_path', 'assets/_generated_pdfs');
}
public function testPdfFilename()
{
$page = $this->objFromFixture('BasePage', 'test-page-one');
$this->assertContains(
'assets/_generated_pdfs/test-page-one-1.pdf',
$page->getPdfFilename(),
'Generated filename for PDF'
);
}
public function testPdfLink()
{
$page = $this->objFromFixture('BasePage', 'test-page-one');
$this->assertContains('test-page-one/downloadpdf', $page->PdfLink(), 'Link to download PDF');
}
public function testHomePagePdfLink()
{
$page = $this->objFromFixture('BasePage', 'home-page');
$this->assertContains('home/downloadpdf', $page->PdfLink(), 'Link to download PDF');
}
public function testPdfLinkDisabled()
{
Config::inst()->update('BasePage', 'pdf_export', false);
$page = $this->objFromFixture('BasePage', 'test-page-one');
$this->assertFalse($page->PdfLink(), 'No PDF link as the functionality is disabled');
}
/**
* Test that the native language name can be returned for the current locale
*
* @see i18n
* @param string $locale
* @param string $expected
* @dataProvider localeProvider
*/
public function testGetSelectedLanguage($locale, $expected)
{
if (!class_exists('Translatable')) {
$this->markTestSkipped('Language tests require Translatable module.');
}
Translatable::set_current_locale($locale);
$page = $this->objFromFixture('BasePage', 'test-page-one');
$this->assertSame($expected, $page->getSelectedLanguage());
}
/**
* @return array[]
*/
public function localeProvider()
{
return array(
array('en_NZ', 'English'),
array('af_ZA', 'Afrikaans'),
array('es_ES', 'espa&ntilde;ol')
);
}
public function tearDown()
{
Config::unnest();
parent::tearDown();
}
}
BasePage:
test-page-one:
Title: Test Page One
home-page:
Title: Home
<?php
class BasePageTests extends SapphireTest {
public static $fixture_file = 'BasePageTests.yml';
public function setUp() {
parent::setUp();
Config::inst()->update('BasePage', 'pdf_export', true);
Config::inst()->update('BasePage', 'generated_pdf_path', 'assets/_generated_pdfs');
}
public function testPdfFilename() {
$page = $this->objFromFixture('Page', 'test-page-one');
$this->assertContains('assets/_generated_pdfs/test-page-one-1.pdf', $page->getPdfFilename(), 'Generated filename for PDF');
}
public function testPdfLink() {
$page = $this->objFromFixture('Page', 'test-page-one');
$this->assertContains('test-page-one/downloadpdf', $page->PdfLink(), 'Link to download PDF');
}
public function testHomePagePdfLink() {
$page = $this->objFromFixture('Page', 'home-page');
$this->assertContains('home/downloadpdf', $page->PdfLink(), 'Link to download PDF');
}
public function testPdfLinkDisabled() {
Config::inst()->update('BasePage', 'pdf_export', false);
$page = $this->objFromFixture('Page', 'test-page-one');
$this->assertFalse($page->PdfLink(), 'No PDF link as the functionality is disabled');
}
}
Page:
test-page-one:
Title: Test Page One
home-page:
Title: Home
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment