2008-10-17 17:21:33 +02:00
< ? php
/**
2012-04-12 08:02:46 +02:00
* @ package framework
2008-10-17 17:21:33 +02:00
* @ subpackage tests
*/
class i18nTextCollectorTest extends SapphireTest {
/**
* @ var string $tmpBasePath Used to write language files .
2012-03-24 04:38:57 +01:00
* We don ' t want to store them inside framework ( or in any web - accessible place )
2008-10-17 17:21:33 +02:00
* in case something goes wrong with the file parsing .
*/
protected $alternateBaseSavePath ;
/**
* @ var string $alternateBasePath Fake webroot with a single module
* / i18ntestmodule which contains some files with _t () calls .
*/
protected $alternateBasePath ;
2011-03-23 04:43:50 +01:00
protected $manifest ;
2012-09-19 12:07:39 +02:00
public function setUp () {
2008-10-17 17:21:33 +02:00
parent :: setUp ();
2011-03-30 08:49:11 +02:00
$this -> alternateBasePath = $this -> getCurrentAbsolutePath () . " /_fakewebroot " ;
2008-10-17 17:21:33 +02:00
$this -> alternateBaseSavePath = TEMP_FOLDER . '/i18nTextCollectorTest_webroot' ;
FileSystem :: makeFolder ( $this -> alternateBaseSavePath );
2008-10-29 22:07:17 +01:00
2011-03-23 04:43:50 +01:00
// Push a class and template loader running from the fake webroot onto
// the stack.
$this -> manifest = new SS_ClassManifest (
$this -> alternateBasePath , false , true , false
2008-10-17 17:21:33 +02:00
);
2010-11-18 20:00:13 +01:00
2011-03-23 04:43:50 +01:00
$manifest = new SS_TemplateManifest ( $this -> alternateBasePath , false , true );
$manifest -> regenerate ( false );
SS_TemplateLoader :: instance () -> pushManifest ( $manifest );
2008-10-17 17:21:33 +02:00
}
2012-09-19 12:07:39 +02:00
public function tearDown () {
2011-03-23 04:43:50 +01:00
SS_TemplateLoader :: instance () -> popManifest ();
2008-12-04 23:38:32 +01:00
parent :: tearDown ();
2008-10-17 17:21:33 +02:00
}
2009-07-09 03:02:43 +02:00
2012-09-19 12:07:39 +02:00
public function testConcatenationInEntityValues () {
2009-07-09 03:02:43 +02:00
$c = new i18nTextCollector ();
$php = <<< PHP
_t (
'Test.CONCATENATED' ,
2009-07-09 04:37:01 +02:00
'Line 1 and ' .
'Line \'2\' and ' .
'Line "3"' ,
2012-04-14 00:16:30 +02:00
2009-07-09 03:02:43 +02:00
'Comment'
);
2009-07-09 04:37:01 +02:00
_t (
'Test.CONCATENATED2' ,
2012-04-18 07:23:57 +02:00
" Line \" 4 \" and " .
2009-07-09 04:37:01 +02:00
" Line 5 " );
2009-07-09 03:02:43 +02:00
PHP ;
$this -> assertEquals (
$c -> collectFromCode ( $php , 'mymodule' ),
array (
2012-04-14 00:16:30 +02:00
'Test.CONCATENATED' => array ( " Line 1 and Line '2' and Line \" 3 \" " , 'Comment' ),
'Test.CONCATENATED2' => array ( " Line \" 4 \" and Line 5 " )
2009-07-09 03:02:43 +02:00
)
);
2012-04-16 07:24:48 +02:00
}
2012-09-19 12:07:39 +02:00
public function testCollectFromNewTemplateSyntaxUsingParserSubclass () {
2012-04-16 07:24:48 +02:00
$c = new i18nTextCollector ();
$html = <<< SS
<% _t ( 'Test.SINGLEQUOTE' , 'Single Quote' ); %>
<% t i18nTestModule . NEWMETHODSIG " New _t method signature test " %>
<% t i18nTestModule . INJECTIONS_0 " Hello { name} { greeting}. But it is late, { goodbye} " name = " Mark " greeting = " welcome " goodbye = " bye " %>
<% t i18nTestModule . INJECTIONS_1 " Hello { name} { greeting}. But it is late, { goodbye} " name = " Paul " greeting = " good you are here " goodbye = " see you " %>
<% t i18nTestModule . INJECTIONS_2 " Hello { name} { greeting}. But it is late, { goodbye} " is " New context (this should be ignored) " name = " Steffen " greeting = " willkommen " goodbye = " wiedersehen " %>
<% t i18nTestModule . INJECTIONS_3 name = " Cat " greeting = 'meow' goodbye = " meow " %>
<% t i18nTestModule . INJECTIONS_4 name = \ $absoluteBaseURL greeting = \ $get_locale goodbye = " global calls " %>
SS ;
$c -> collectFromTemplate ( $html , 'mymodule' , 'Test' );
$this -> assertEquals (
$c -> collectFromTemplate ( $html , 'mymodule' , 'Test' ),
array (
2012-04-18 07:23:57 +02:00
'Test.SINGLEQUOTE' => array ( 'Single Quote' ),
2012-04-16 07:24:48 +02:00
'i18nTestModule.NEWMETHODSIG' => array ( " New _t method signature test " , null , null ),
'i18nTestModule.INJECTIONS_0' => array ( " Hello { name} { greeting}. But it is late, { goodbye} " , null , null ),
'i18nTestModule.INJECTIONS_1' => array ( " Hello { name} { greeting}. But it is late, { goodbye} " , null , null ),
'i18nTestModule.INJECTIONS_2' => array ( " Hello { name} { greeting}. But it is late, { goodbye} " , null , " New context (this should be ignored) " ),
'i18nTestModule.INJECTIONS_3' => array ( null , null , null ),
'i18nTestModule.INJECTIONS_4' => array ( null , null , null ),
)
);
}
2012-09-19 12:07:39 +02:00
public function testCollectFromTemplateSimple () {
2008-10-17 17:21:33 +02:00
$c = new i18nTextCollector ();
$html = <<< SS
<% _t ( 'Test.SINGLEQUOTE' , 'Single Quote' ); %>
SS ;
$this -> assertEquals (
$c -> collectFromTemplate ( $html , 'mymodule' , 'Test' ),
array (
2012-04-14 00:16:30 +02:00
'Test.SINGLEQUOTE' => array ( 'Single Quote' )
2008-10-17 17:21:33 +02:00
)
);
$html = <<< SS
<% _t ( " Test.DOUBLEQUOTE " , " Double Quote and Spaces " ); %>
SS ;
$this -> assertEquals (
$c -> collectFromTemplate ( $html , 'mymodule' , 'Test' ),
array (
2012-04-14 00:16:30 +02:00
'Test.DOUBLEQUOTE' => array ( " Double Quote and Spaces " )
2008-10-17 17:21:33 +02:00
)
);
$html = <<< SS
<% _t ( " Test.NOSEMICOLON " , " No Semicolon " ) %>
SS ;
$this -> assertEquals (
$c -> collectFromTemplate ( $html , 'mymodule' , 'Test' ),
array (
2012-04-14 00:16:30 +02:00
'Test.NOSEMICOLON' => array ( " No Semicolon " )
2008-10-17 17:21:33 +02:00
)
);
}
2012-09-19 12:07:39 +02:00
public function testCollectFromTemplateAdvanced () {
2008-10-17 17:21:33 +02:00
$c = new i18nTextCollector ();
$html = <<< SS
<% _t (
'NEWLINES' ,
'New Lines'
) %>
SS ;
$this -> assertEquals (
$c -> collectFromTemplate ( $html , 'mymodule' , 'Test' ),
array (
2012-04-14 00:16:30 +02:00
'Test.NEWLINES' => array ( " New Lines " )
2008-10-17 17:21:33 +02:00
)
);
$html = <<< SS
<% _t (
'Test.PRIOANDCOMMENT' ,
' Prio and Value with "Double Quotes"' ,
'Comment with "Double Quotes"'
) %>
SS ;
$this -> assertEquals (
$c -> collectFromTemplate ( $html , 'mymodule' , 'Test' ),
array (
2012-04-14 00:16:30 +02:00
'Test.PRIOANDCOMMENT' => array ( ' Prio and Value with "Double Quotes"' , 'Comment with "Double Quotes"' )
2008-10-17 17:21:33 +02:00
)
);
$html = <<< SS
<% _t (
'Test.PRIOANDCOMMENT' ,
" Prio and Value with 'Single Quotes' " ,
2012-04-14 00:16:30 +02:00
2008-10-17 17:21:33 +02:00
" Comment with 'Single Quotes' "
) %>
SS ;
$this -> assertEquals (
$c -> collectFromTemplate ( $html , 'mymodule' , 'Test' ),
array (
2012-04-14 00:16:30 +02:00
'Test.PRIOANDCOMMENT' => array ( " Prio and Value with 'Single Quotes' " , " Comment with 'Single Quotes' " )
2008-10-17 17:21:33 +02:00
)
);
}
2012-09-19 12:07:39 +02:00
public function testCollectFromCodeSimple () {
2008-10-17 17:21:33 +02:00
$c = new i18nTextCollector ();
$php = <<< PHP
_t ( 'Test.SINGLEQUOTE' , 'Single Quote' );
PHP ;
$this -> assertEquals (
$c -> collectFromCode ( $php , 'mymodule' ),
array (
2012-04-14 00:16:30 +02:00
'Test.SINGLEQUOTE' => array ( 'Single Quote' )
2008-10-17 17:21:33 +02:00
)
);
$php = <<< PHP
_t ( " Test.DOUBLEQUOTE " , " Double Quote and Spaces " );
PHP ;
$this -> assertEquals (
$c -> collectFromCode ( $php , 'mymodule' ),
array (
2012-04-14 00:16:30 +02:00
'Test.DOUBLEQUOTE' => array ( " Double Quote and Spaces " )
2008-10-17 17:21:33 +02:00
)
);
}
2012-09-19 12:07:39 +02:00
public function testCollectFromCodeAdvanced () {
2008-10-17 17:21:33 +02:00
$c = new i18nTextCollector ();
$php = <<< PHP
_t (
'Test.NEWLINES' ,
'New Lines'
);
PHP ;
$this -> assertEquals (
$c -> collectFromCode ( $php , 'mymodule' ),
array (
2012-04-14 00:16:30 +02:00
'Test.NEWLINES' => array ( " New Lines " )
2008-10-17 17:21:33 +02:00
)
);
$php = <<< PHP
_t (
'Test.PRIOANDCOMMENT' ,
' Value with "Double Quotes"' ,
2012-04-14 00:16:30 +02:00
2008-10-17 17:21:33 +02:00
'Comment with "Double Quotes"'
);
PHP ;
$this -> assertEquals (
$c -> collectFromCode ( $php , 'mymodule' ),
array (
2012-04-14 00:16:30 +02:00
'Test.PRIOANDCOMMENT' => array ( ' Value with "Double Quotes"' , 'Comment with "Double Quotes"' )
2008-10-17 17:21:33 +02:00
)
);
$php = <<< PHP
_t (
'Test.PRIOANDCOMMENT' ,
" Value with 'Single Quotes' " ,
2012-04-14 00:16:30 +02:00
2008-10-17 17:21:33 +02:00
" Comment with 'Single Quotes' "
);
PHP ;
$this -> assertEquals (
$c -> collectFromCode ( $php , 'mymodule' ),
array (
2012-04-14 00:16:30 +02:00
'Test.PRIOANDCOMMENT' => array ( " Value with 'Single Quotes' " , " Comment with 'Single Quotes' " )
2008-10-17 17:21:33 +02:00
)
);
$php = <<< PHP
_t (
'Test.PRIOANDCOMMENT' ,
'Value with \'Escaped Single Quotes\''
);
PHP ;
$this -> assertEquals (
$c -> collectFromCode ( $php , 'mymodule' ),
array (
2012-04-14 00:16:30 +02:00
'Test.PRIOANDCOMMENT' => array ( " Value with 'Escaped Single Quotes' " )
2008-10-17 17:21:33 +02:00
)
);
$php = <<< PHP
_t (
'Test.PRIOANDCOMMENT' ,
" Doublequoted Value with 'Unescaped Single Quotes' "
);
PHP ;
$this -> assertEquals (
$c -> collectFromCode ( $php , 'mymodule' ),
array (
2012-04-14 00:16:30 +02:00
'Test.PRIOANDCOMMENT' => array ( " Doublequoted Value with 'Unescaped Single Quotes' " )
2008-10-17 17:21:33 +02:00
)
);
}
2008-11-01 19:55:27 +01:00
2012-09-19 12:07:39 +02:00
public function testNewlinesInEntityValues () {
2008-11-01 19:55:27 +01:00
$c = new i18nTextCollector ();
$php = <<< PHP
_t (
'Test.NEWLINESINGLEQUOTE' ,
' Line 1
Line 2 '
);
PHP ;
2009-11-21 02:43:54 +01:00
$eol = PHP_EOL ;
2008-11-01 19:55:27 +01:00
$this -> assertEquals (
$c -> collectFromCode ( $php , 'mymodule' ),
array (
2012-04-14 00:16:30 +02:00
'Test.NEWLINESINGLEQUOTE' => array ( " Line 1 { $eol } Line 2 " )
2008-11-01 19:55:27 +01:00
)
);
$php = <<< PHP
_t (
'Test.NEWLINEDOUBLEQUOTE' ,
" Line 1
Line 2 "
);
PHP ;
$this -> assertEquals (
$c -> collectFromCode ( $php , 'mymodule' ),
array (
2012-04-14 00:16:30 +02:00
'Test.NEWLINEDOUBLEQUOTE' => array ( " Line 1 { $eol } Line 2 " )
2008-11-01 19:55:27 +01:00
)
);
}
2009-07-09 03:02:43 +02:00
2012-04-16 07:24:48 +02:00
/**
* Test extracting entities from the new _t method signature
*/
2012-09-19 12:07:39 +02:00
public function testCollectFromCodeNewSignature () {
2012-04-16 07:24:48 +02:00
$c = new i18nTextCollector ();
$php = <<< PHP
_t ( 'i18nTestModule.NEWMETHODSIG' , " New _t method signature test " );
_t ( 'i18nTestModule.INJECTIONS1' , '_DOES_NOT_EXIST' , " Hello { name} { greeting}. But it is late, { goodbye} " , array ( " name " => " Mark " , " greeting " => " welcome " , " goodbye " => " bye " ));
_t ( 'i18nTestModule.INJECTIONS2' , " Hello { name} { greeting}. But it is late, { goodbye} " , array ( " name " => " Paul " , " greeting " => " good you are here " , " goodbye " => " see you " ));
_t ( " i18nTestModule.INJECTIONS3 " , " Hello { name} { greeting}. But it is late, { goodbye} " , " New context (this should be ignored) " , array ( " name " => " Steffen " , " greeting " => " willkommen " , " goodbye " => " wiedersehen " ));
_t ( 'i18nTestModule.INJECTIONS4' , array ( " name " => " Cat " , " greeting " => " meow " , " goodbye " => " meow " ));
PHP ;
$collectedTranslatables = $c -> collectFromCode ( $php , 'mymodule' );
$expectedArray = ( array (
2012-04-18 07:23:57 +02:00
'i18nTestModule.NEWMETHODSIG' => array ( " New _t method signature test " ),
'i18nTestModule.INJECTIONS1' => array ( " _DOES_NOT_EXIST " , " Hello { name} { greeting}. But it is late, { goodbye} " ),
'i18nTestModule.INJECTIONS2' => array ( " Hello { name} { greeting}. But it is late, { goodbye} " ),
'i18nTestModule.INJECTIONS3' => array ( " Hello { name} { greeting}. But it is late, { goodbye} " , " New context (this should be ignored) " ),
2012-04-16 07:24:48 +02:00
));
ksort ( $expectedArray );
$this -> assertEquals ( $collectedTranslatables , $expectedArray );
}
2008-10-17 17:21:33 +02:00
/**
* Input for langArrayCodeForEntitySpec () should be suitable for insertion
* into single - quoted strings , so needs to be escaped already .
*/
2012-09-19 12:07:39 +02:00
public function testPhpWriterLangArrayCodeForEntity () {
2012-04-14 00:16:30 +02:00
$c = new i18nTextCollector_Writer_Php ();
2008-10-17 17:21:33 +02:00
$this -> assertEquals (
2012-04-14 00:16:30 +02:00
$c -> langArrayCodeForEntitySpec ( 'Test.SIMPLE' , array ( 'Simple Value' ), 'en_US' ),
" \$ lang['en_US']['Test']['SIMPLE'] = 'Simple Value'; " . PHP_EOL
2008-10-17 17:21:33 +02:00
);
$this -> assertEquals (
// single quotes should be properly escaped by the parser already
2012-04-14 00:16:30 +02:00
$c -> langArrayCodeForEntitySpec ( 'Test.ESCAPEDSINGLEQUOTES' , array ( " Value with 'Escaped Single Quotes' " ), 'en_US' ),
" \$ lang['en_US']['Test']['ESCAPEDSINGLEQUOTES'] = 'Value with \ 'Escaped Single Quotes \ ''; " . PHP_EOL
2008-10-17 17:21:33 +02:00
);
$this -> assertEquals (
2012-04-14 00:16:30 +02:00
$c -> langArrayCodeForEntitySpec ( 'Test.DOUBLEQUOTES' , array ( 'Value with "Double Quotes"' ), 'en_US' ),
" \$ lang['en_US']['Test']['DOUBLEQUOTES'] = 'Value with \" Double Quotes \" '; " . PHP_EOL
2008-10-17 17:21:33 +02:00
);
$php = <<< PHP
2012-04-14 00:16:30 +02:00
\ $lang [ 'en_US' ][ 'Test' ][ 'PRIOANDCOMMENT' ] = array (
0 => 'Value with \'Single Quotes\'' ,
1 => 'Comment with \'Single Quotes\'' ,
2008-10-17 17:21:33 +02:00
);
PHP ;
$this -> assertEquals (
2012-04-14 00:16:30 +02:00
$c -> langArrayCodeForEntitySpec ( 'Test.PRIOANDCOMMENT' , array ( " Value with 'Single Quotes' " , " Comment with 'Single Quotes' " ), 'en_US' ),
2008-10-17 17:21:33 +02:00
$php
);
$php = <<< PHP
2012-04-14 00:16:30 +02:00
\ $lang [ 'en_US' ][ 'Test' ][ 'PRIOANDCOMMENT' ] = array (
0 => 'Value with "Double Quotes"' ,
1 => 'Comment with "Double Quotes"' ,
2008-10-17 17:21:33 +02:00
);
PHP ;
$this -> assertEquals (
2012-04-14 00:16:30 +02:00
$c -> langArrayCodeForEntitySpec ( 'Test.PRIOANDCOMMENT' , array ( 'Value with "Double Quotes"' , 'Comment with "Double Quotes"' ), 'en_US' ),
2008-10-17 17:21:33 +02:00
$php
);
}
2012-04-14 01:01:59 +02:00
/**
* @ todo Should be in a separate test suite , but don ' t want to duplicate setup logic
*/
2012-09-19 12:07:39 +02:00
public function testYamlWriter () {
2012-04-14 01:01:59 +02:00
$writer = new i18nTextCollector_Writer_RailsYaml ();
$entities = array (
'Level1.Level2.EntityName' => array ( 'Text' , 'Context' ),
'Level1.OtherEntityName' => array ( 'Other Text' , 'Other Context' ),
);
$yaml = <<< YAML
de :
Level1 :
Level2 :
EntityName : Text
OtherEntityName : 'Other Text'
YAML ;
$this -> assertEquals ( $yaml , $writer -> getYaml ( $entities , 'de' ));
}
2008-10-17 17:21:33 +02:00
2012-09-19 12:07:39 +02:00
public function testCollectFromIncludedTemplates () {
2008-10-17 17:21:33 +02:00
$c = new i18nTextCollector ();
2008-10-29 22:07:17 +01:00
$templateFilePath = $this -> alternateBasePath . '/i18ntestmodule/templates/Layout/i18nTestModule.ss' ;
2008-10-17 19:44:14 +02:00
$html = file_get_contents ( $templateFilePath );
2008-11-06 03:50:14 +01:00
$matches = $c -> collectFromTemplate ( $html , 'mymodule' , 'RandomNamespace' );
/*
$this -> assertArrayHasKey ( 'i18nTestModule.ss.LAYOUTTEMPLATENONAMESPACE' , $matches );
2008-10-17 17:21:33 +02:00
$this -> assertEquals (
2008-11-06 03:50:14 +01:00
$matches [ 'i18nTestModule.ss.LAYOUTTEMPLATENONAMESPACE' ],
2012-04-14 00:16:30 +02:00
array ( 'Layout Template no namespace' )
2008-11-06 03:50:14 +01:00
);
*/
$this -> assertArrayHasKey ( 'RandomNamespace.SPRINTFNONAMESPACE' , $matches );
$this -> assertEquals (
$matches [ 'RandomNamespace.SPRINTFNONAMESPACE' ],
2012-04-14 00:16:30 +02:00
array ( 'My replacement no namespace: %s' )
2008-11-06 03:50:14 +01:00
);
$this -> assertArrayHasKey ( 'i18nTestModule.LAYOUTTEMPLATE' , $matches );
$this -> assertEquals (
$matches [ 'i18nTestModule.LAYOUTTEMPLATE' ],
2012-04-14 00:16:30 +02:00
array ( 'Layout Template' )
2008-11-06 03:50:14 +01:00
);
$this -> assertArrayHasKey ( 'i18nTestModule.SPRINTFNAMESPACE' , $matches );
$this -> assertEquals (
$matches [ 'i18nTestModule.SPRINTFNAMESPACE' ],
2012-04-14 00:16:30 +02:00
array ( 'My replacement: %s' )
2008-11-06 03:50:14 +01:00
);
$this -> assertArrayHasKey ( 'i18nTestModule.WITHNAMESPACE' , $matches );
$this -> assertEquals (
$matches [ 'i18nTestModule.WITHNAMESPACE' ],
2012-04-14 00:16:30 +02:00
array ( 'Include Entity with Namespace' )
2008-11-06 03:50:14 +01:00
);
$this -> assertArrayHasKey ( 'i18nTestModuleInclude.ss.NONAMESPACE' , $matches );
$this -> assertEquals (
$matches [ 'i18nTestModuleInclude.ss.NONAMESPACE' ],
2012-04-14 00:16:30 +02:00
array ( 'Include Entity without Namespace' )
2008-11-06 03:50:14 +01:00
);
$this -> assertArrayHasKey ( 'i18nTestModuleInclude.ss.SPRINTFINCLUDENAMESPACE' , $matches );
$this -> assertEquals (
$matches [ 'i18nTestModuleInclude.ss.SPRINTFINCLUDENAMESPACE' ],
2012-04-14 00:16:30 +02:00
array ( 'My include replacement: %s' )
2008-11-06 03:50:14 +01:00
);
$this -> assertArrayHasKey ( 'i18nTestModuleInclude.ss.SPRINTFINCLUDENONAMESPACE' , $matches );
$this -> assertEquals (
$matches [ 'i18nTestModuleInclude.ss.SPRINTFINCLUDENONAMESPACE' ],
2012-04-14 00:16:30 +02:00
array ( 'My include replacement no namespace: %s' )
2008-10-17 17:21:33 +02:00
);
}
2012-09-19 12:07:39 +02:00
public function testCollectFromThemesTemplates () {
2010-11-18 20:00:13 +01:00
$c = new i18nTextCollector ();
2011-03-23 04:43:50 +01:00
$theme = SSViewer :: current_theme ();
SSViewer :: set_theme ( 'testtheme1' );
2010-11-18 20:00:13 +01:00
$templateFilePath = $this -> alternateBasePath . '/themes/testtheme1/templates/Layout/i18nTestTheme1.ss' ;
$html = file_get_contents ( $templateFilePath );
$matches = $c -> collectFromTemplate ( $html , 'themes/testtheme1' , 'i18nTestTheme1.ss' );
// all entities from i18nTestTheme1.ss
$this -> assertEquals (
$matches [ 'i18nTestTheme1.LAYOUTTEMPLATE' ],
2012-04-14 00:16:30 +02:00
array ( 'Theme1 Layout Template' )
2010-11-18 20:00:13 +01:00
);
$this -> assertArrayHasKey ( 'i18nTestTheme1.ss.LAYOUTTEMPLATENONAMESPACE' , $matches );
$this -> assertEquals (
$matches [ 'i18nTestTheme1.ss.LAYOUTTEMPLATENONAMESPACE' ],
2012-04-14 00:16:30 +02:00
array ( 'Theme1 Layout Template no namespace' )
2010-11-18 20:00:13 +01:00
);
$this -> assertEquals (
$matches [ 'i18nTestTheme1.SPRINTFNAMESPACE' ],
2012-04-14 00:16:30 +02:00
array ( 'Theme1 My replacement: %s' )
2010-11-18 20:00:13 +01:00
);
$this -> assertArrayHasKey ( 'i18nTestTheme1.ss.SPRINTFNONAMESPACE' , $matches );
$this -> assertEquals (
$matches [ 'i18nTestTheme1.ss.SPRINTFNONAMESPACE' ],
2012-04-14 00:16:30 +02:00
array ( 'Theme1 My replacement no namespace: %s' )
2010-11-18 20:00:13 +01:00
);
// all entities from i18nTestTheme1Include.ss
$this -> assertEquals (
$matches [ 'i18nTestTheme1Include.WITHNAMESPACE' ],
2012-04-14 00:16:30 +02:00
array ( 'Theme1 Include Entity with Namespace' )
2010-11-18 20:00:13 +01:00
);
$this -> assertArrayHasKey ( 'i18nTestTheme1Include.ss.NONAMESPACE' , $matches );
$this -> assertEquals (
$matches [ 'i18nTestTheme1Include.ss.NONAMESPACE' ],
2012-04-14 00:16:30 +02:00
array ( 'Theme1 Include Entity without Namespace' )
2010-11-18 20:00:13 +01:00
);
$this -> assertEquals (
$matches [ 'i18nTestTheme1Include.SPRINTFINCLUDENAMESPACE' ],
2012-04-14 00:16:30 +02:00
array ( 'Theme1 My include replacement: %s' )
2010-11-18 20:00:13 +01:00
);
$this -> assertArrayHasKey ( 'i18nTestTheme1Include.ss.SPRINTFINCLUDENONAMESPACE' , $matches );
$this -> assertEquals (
$matches [ 'i18nTestTheme1Include.ss.SPRINTFINCLUDENONAMESPACE' ],
2012-04-14 00:16:30 +02:00
array ( 'Theme1 My include replacement no namespace: %s' )
2010-11-18 20:00:13 +01:00
);
2011-03-23 04:43:50 +01:00
SSViewer :: set_theme ( $theme );
2010-11-18 20:00:13 +01:00
}
2012-09-19 12:07:39 +02:00
public function testCollectFromFilesystemAndWriteMasterTables () {
2010-10-19 00:33:41 +02:00
$defaultlocal = i18n :: default_locale ();
$local = i18n :: get_locale ();
i18n :: set_locale ( 'en_US' ); //set the locale to the US locale expected in the asserts
i18n :: set_default_locale ( 'en_US' );
2008-10-17 17:21:33 +02:00
$c = new i18nTextCollector ();
2012-04-14 00:16:30 +02:00
$c -> setWriter ( new i18nTextCollector_Writer_Php ());
2008-10-17 17:21:33 +02:00
$c -> basePath = $this -> alternateBasePath ;
$c -> baseSavePath = $this -> alternateBaseSavePath ;
2008-10-17 19:44:14 +02:00
$c -> run ();
2008-10-17 17:21:33 +02:00
2008-10-17 19:44:14 +02:00
// i18ntestmodule
2008-10-17 17:21:33 +02:00
$moduleLangFile = " { $this -> alternateBaseSavePath } /i18ntestmodule/lang/ " . $c -> getDefaultLocale () . '.php' ;
$this -> assertTrue (
file_exists ( $moduleLangFile ),
'Master language file can be written to modules /lang folder'
);
2008-11-01 23:47:02 +01:00
$moduleLangFileContent = file_get_contents ( $moduleLangFile );
$this -> assertContains (
2012-04-14 00:16:30 +02:00
" \$ lang['en']['i18nTestModule']['ADDITION'] = 'Addition'; " ,
2008-11-01 23:47:02 +01:00
$moduleLangFileContent
);
$this -> assertContains (
2012-04-14 00:16:30 +02:00
" \$ lang['en']['i18nTestModule']['ENTITY'] = array (
0 => 'Entity with \"Double Quotes\"' ,
1 => 'Comment for entity' ,
2008-11-01 23:47:02 +01:00
); " ,
$moduleLangFileContent
);
$this -> assertContains (
2012-04-14 00:16:30 +02:00
" \$ lang['en']['i18nTestModule']['MAINTEMPLATE'] = 'Main Template'; " ,
2008-11-01 23:47:02 +01:00
$moduleLangFileContent
);
$this -> assertContains (
2012-04-14 00:16:30 +02:00
" \$ lang['en']['i18nTestModule']['OTHERENTITY'] = 'Other Entity'; " ,
2008-11-01 23:47:02 +01:00
$moduleLangFileContent
);
$this -> assertContains (
2012-04-14 00:16:30 +02:00
" \$ lang['en']['i18nTestModule']['WITHNAMESPACE'] = 'Include Entity with Namespace'; " ,
2008-11-01 23:47:02 +01:00
$moduleLangFileContent
);
$this -> assertContains (
2012-04-14 00:16:30 +02:00
" \$ lang['en']['i18nTestModuleInclude.ss']['NONAMESPACE'] = 'Include Entity without Namespace'; " ,
2008-11-01 23:47:02 +01:00
$moduleLangFileContent
2008-10-17 19:44:14 +02:00
);
// i18nothermodule
2008-11-01 23:47:02 +01:00
$otherModuleLangFile = " { $this -> alternateBaseSavePath } /i18nothermodule/lang/ " . $c -> getDefaultLocale () . '.php' ;
2008-10-17 19:44:14 +02:00
$this -> assertTrue (
2008-11-01 23:47:02 +01:00
file_exists ( $otherModuleLangFile ),
2008-10-17 19:44:14 +02:00
'Master language file can be written to modules /lang folder'
);
2008-11-01 23:47:02 +01:00
$otherModuleLangFileContent = file_get_contents ( $otherModuleLangFile );
$this -> assertContains (
2012-04-14 00:16:30 +02:00
" \$ lang['en']['i18nOtherModule']['ENTITY'] = 'Other Module Entity'; " ,
2008-11-01 23:47:02 +01:00
$otherModuleLangFileContent
);
$this -> assertContains (
2012-04-14 00:16:30 +02:00
" \$ lang['en']['i18nOtherModule']['MAINTEMPLATE'] = 'Main Template Other Module'; " ,
2008-11-01 23:47:02 +01:00
$otherModuleLangFileContent
2008-10-17 17:21:33 +02:00
);
2010-11-18 20:00:13 +01:00
// testtheme1
$theme1LangFile = " { $this -> alternateBaseSavePath } /themes/testtheme1/lang/ " . $c -> getDefaultLocale () . '.php' ;
$this -> assertTrue (
file_exists ( $theme1LangFile ),
'Master theme language file can be written to themes/testtheme1 /lang folder'
);
$theme1LangFileContent = file_get_contents ( $theme1LangFile );
$this -> assertContains (
2012-04-14 00:16:30 +02:00
" \$ lang['en']['i18nTestTheme1']['MAINTEMPLATE'] = 'Theme1 Main Template'; " ,
2010-11-18 20:00:13 +01:00
$theme1LangFileContent
);
$this -> assertContains (
2012-04-14 00:16:30 +02:00
" \$ lang['en']['i18nTestTheme1']['LAYOUTTEMPLATE'] = 'Theme1 Layout Template'; " ,
2010-11-18 20:00:13 +01:00
$theme1LangFileContent
);
$this -> assertContains (
2012-04-14 00:16:30 +02:00
" \$ lang['en']['i18nTestTheme1']['SPRINTFNAMESPACE'] = 'Theme1 My replacement: %s'; " ,
2010-11-18 20:00:13 +01:00
$theme1LangFileContent
);
$this -> assertContains (
2012-04-14 00:16:30 +02:00
" \$ lang['en']['i18nTestTheme1.ss']['LAYOUTTEMPLATENONAMESPACE'] = 'Theme1 Layout Template no namespace'; " ,
2010-11-18 20:00:13 +01:00
$theme1LangFileContent
);
$this -> assertContains (
2012-04-14 00:16:30 +02:00
" \$ lang['en']['i18nTestTheme1.ss']['SPRINTFNONAMESPACE'] = 'Theme1 My replacement no namespace: %s'; " ,
2010-11-18 20:00:13 +01:00
$theme1LangFileContent
);
$this -> assertContains (
2012-04-14 00:16:30 +02:00
" \$ lang['en']['i18nTestTheme1Include']['SPRINTFINCLUDENAMESPACE'] = 'Theme1 My include replacement: %s'; " ,
2010-11-18 20:00:13 +01:00
$theme1LangFileContent
);
$this -> assertContains (
2012-04-14 00:16:30 +02:00
" \$ lang['en']['i18nTestTheme1Include']['WITHNAMESPACE'] = 'Theme1 Include Entity with Namespace'; " ,
2010-11-18 20:00:13 +01:00
$theme1LangFileContent
);
$this -> assertContains (
2012-04-14 00:16:30 +02:00
" \$ lang['en']['i18nTestTheme1Include.ss']['NONAMESPACE'] = 'Theme1 Include Entity without Namespace'; " ,
2010-11-18 20:00:13 +01:00
$theme1LangFileContent
);
$this -> assertContains (
2012-04-14 00:16:30 +02:00
" \$ lang['en']['i18nTestTheme1Include.ss']['SPRINTFINCLUDENONAMESPACE'] = 'Theme1 My include replacement no namespace: %s'; " ,
2010-11-18 20:00:13 +01:00
$theme1LangFileContent
);
// testtheme2
$theme2LangFile = " { $this -> alternateBaseSavePath } /themes/testtheme2/lang/ " . $c -> getDefaultLocale () . '.php' ;
$this -> assertTrue (
file_exists ( $theme2LangFile ),
'Master theme language file can be written to themes/testtheme2 /lang folder'
);
$theme2LangFileContent = file_get_contents ( $theme2LangFile );
$this -> assertContains (
2012-04-14 00:16:30 +02:00
" \$ lang['en']['i18nTestTheme2']['MAINTEMPLATE'] = 'Theme2 Main Template'; " ,
2010-11-18 20:00:13 +01:00
$theme2LangFileContent
);
2010-10-19 00:33:41 +02:00
i18n :: set_locale ( $local ); //set the locale to the US locale expected in the asserts
+ i18n :: set_default_locale ( $defaultlocal );
2008-10-17 17:21:33 +02:00
}
2008-10-29 22:07:17 +01:00
2012-09-19 12:07:39 +02:00
public function testCollectFromEntityProvidersInCustomObject () {
2008-10-29 22:07:17 +01:00
$c = new i18nTextCollector ();
2011-03-30 08:49:11 +02:00
$filePath = $this -> getCurrentAbsolutePath () . '/i18nTextCollectorTestMyObject.php' ;
2008-10-29 22:07:17 +01:00
$matches = $c -> collectFromEntityProviders ( $filePath );
$this -> assertEquals (
array_keys ( $matches ),
array (
'i18nTextCollectorTestMyObject.PLURALNAME' ,
'i18nTextCollectorTestMyObject.SINGULARNAME' ,
)
);
$this -> assertEquals (
'My Object' ,
$matches [ 'i18nTextCollectorTestMyObject.SINGULARNAME' ][ 0 ]
);
}
2008-10-17 17:21:33 +02:00
}