diff --git a/classes/WDTSLanguageOptions.php b/classes/WDTSLanguageOptions.php index 888dfa0..3928f29 100644 --- a/classes/WDTSLanguageOptions.php +++ b/classes/WDTSLanguageOptions.php @@ -1,13 +1,15 @@ 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; + } } ?> diff --git a/classes/WhatDidTheySayAdmin.php b/classes/WhatDidTheySayAdmin.php index 6213731..b38e419 100644 --- a/classes/WhatDidTheySayAdmin.php +++ b/classes/WhatDidTheySayAdmin.php @@ -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'; } diff --git a/test/WDTSLanguageOptionsTest.php b/test/WDTSLanguageOptionsTest.php new file mode 100644 index 0000000..ad5a780 --- /dev/null +++ b/test/WDTSLanguageOptionsTest.php @@ -0,0 +1,126 @@ +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) + ); + } +} + +?> \ No newline at end of file