language renaming

This commit is contained in:
John Bintz 2009-09-06 18:15:37 -04:00
parent 1fafe2a3da
commit 354a8238a8
3 changed files with 171 additions and 5 deletions

View File

@ -1,13 +1,15 @@
<?php
class WDTSLanguageOptions {
var $key = "what-did-they-say-options";
/**
* Get the default transcript language for this blog.
* @return string The language code representing the default language.
*/
function get_default_language() {
$language = false;
$options = get_option('what-did-they-say-options');
$options = get_option($this->key);
foreach ($options['languages'] as $code => $info) {
if (is_null($language)) { $language = $code; }
if ($info['default']) { $language = $code; break; }
@ -21,7 +23,7 @@ class WDTSLanguageOptions {
* @return string|false The name of the language as defined in the options, or false if the language was not found.
*/
function get_language_name($language) {
$options = get_option('what-did-they-say-options');
$options = get_option($this->key);
if (isset($options['languages'][$language])) {
return $options['languages'][$language]['name'];
@ -35,10 +37,49 @@ class WDTSLanguageOptions {
* @return array An array of languages.
*/
function get_languages() {
$options = get_option('what-did-they-say-options');
$options = get_option($this->key);
return $options['languages'];
}
function delete_language($code_to_delete) {
$options = get_option($this->key);
$new_languages = array();
foreach ($options['languages'] as $code => $info) {
if ($code != $code_to_delete) { $new_languages[$code] = $info; }
}
$options['languages'] = $new_languages;
update_option($this->key, $options);
}
function add_language($code, $info) {
$options = get_option($this->key);
$options['languages'][$code] = $info;
update_option($this->key, $options);
}
function rename_language($code_to_rename, $new_name) {
$options = get_option($this->key);
$found = false;
if (!empty($code_to_rename) && !empty($new_name)) {
$new_languages = array();
foreach ($options['languages'] as $code => $info) {
if ($code == $code_to_rename) {
$info['name'] = $new_name;
$found = true;
}
$new_languages[$code] = $info;
}
$options['languages'] = $new_languages;
update_option($this->key, $options);
}
return $found;
}
}
?>

View File

@ -29,8 +29,7 @@ class WhatDidTheySayAdmin {
* Initialize the admin interface.
* @param WhatDidTheySay $what_did_they_say The WhatDidTheySay object to use for all transcript transactions.
*/
function WhatDidTheySayAdmin($what_did_they_say = null) {
$this->what_did_they_say = $what_did_they_say;
function WhatDidTheySayAdmin() {
$this->language_file = dirname(__FILE__) . '/../data/lsr-language.txt';
}

View File

@ -0,0 +1,126 @@
<?php
require_once('PHPUnit/Framework.php');
require_once(dirname(__FILE__) . '/../classes/WDTSLanguageOptions.php');
require_once(dirname(__FILE__) . '/../../mockpress/mockpress.php');
class WDTSLanguageOptionsTest extends PHPUnit_Framework_TestCase {
function setUp() {
$this->l = new WDTSLanguageOptions();
}
function testGetDefaultLanguage() {
update_option($this->l->key, array(
'languages' => array(
'en' => array('default' => false),
'de' => array('default' => true)
)
));
$this->assertEquals('de', $this->l->get_default_language());
}
function testGetLanguageName() {
update_option($this->l->key, array(
'languages' => array(
'en' => array('name' => 'English')
)
));
$this->assertEquals('English', $this->l->get_language_name('en'));
}
function testGetLanguages() {
update_option($this->l->key, array(
'languages' => array(
'en' => array('name' => 'English'),
'de' => array('name' => 'German')
)
));
$this->assertEquals(array(
'en' => array('name' => 'English'),
'de' => array('name' => 'German')
),
$this->l->get_languages()
);
}
function testDeleteLanguage() {
update_option($this->l->key, array(
'languages' => array(
'en' => array('name' => 'English'),
'de' => array('name' => 'German')
)
));
$this->l->delete_language('en');
$this->assertEquals(array(
'languages' => array(
'de' => array('name' => 'German')
)
),
get_option($this->l->key)
);
}
function testAddLanguage() {
update_option($this->l->key, array(
'languages' => array(
'en' => array('name' => 'English')
)
));
$this->l->add_language('de', array('name' => 'German'));
$this->assertEquals(array(
'languages' => array(
'en' => array('name' => 'English'),
'de' => array('name' => 'German')
)
),
get_option($this->l->key)
);
}
function providerTestRenameLanguage() {
return array(
array('en', 'Anglais', true),
array('de', 'Anglais', false),
array('en', '', false),
array('', 'Anglais', false),
);
}
/**
* @dataProvider providerTestRenameLanguage
*/
function testRenameLanguage($code_to_rename, $new_name, $expected_result) {
update_option($this->l->key, array(
'languages' => array(
'en' => array('name' => 'English')
)
));
$this->assertEquals(
$expected_result,
$result = $this->l->rename_language($code_to_rename, $new_name)
);
$check = array(
'en' => array('name' => 'English')
);
if ($expected_result) {
$check['en']['name'] = $new_name;
}
$this->assertEquals(array(
'languages' => $check
),
get_option($this->l->key)
);
}
}
?>