language CRUD

This commit is contained in:
John Bintz 2009-09-06 18:28:43 -04:00
parent 354a8238a8
commit 5a8c7fd8f6
2 changed files with 71 additions and 23 deletions

View File

@ -45,19 +45,31 @@ class WDTSLanguageOptions {
function delete_language($code_to_delete) { function delete_language($code_to_delete) {
$options = get_option($this->key); $options = get_option($this->key);
$new_languages = array(); $did_delete = false;
foreach ($options['languages'] as $code => $info) { if (isset($options['languages'][$code_to_delete])) {
if ($code != $code_to_delete) { $new_languages[$code] = $info; } $did_delete = true;
unset($options['languages'][$code_to_delete]);
} }
$options['languages'] = $new_languages;
update_option($this->key, $options); update_option($this->key, $options);
return $did_delete;
} }
function add_language($code, $info) { function add_language($code, $info) {
$options = get_option($this->key); $options = get_option($this->key);
$result = false;
if (!empty($code) && is_array($info)) {
if (!isset($options['languages'][$code])) {
if (!empty($info['name'])) {
$options['languages'][$code] = $info; $options['languages'][$code] = $info;
$result = true;
}
}
}
update_option($this->key, $options); update_option($this->key, $options);
return $result;
} }
function rename_language($code_to_rename, $new_name) { function rename_language($code_to_rename, $new_name) {

View File

@ -46,39 +46,75 @@ class WDTSLanguageOptionsTest extends PHPUnit_Framework_TestCase {
); );
} }
function testDeleteLanguage() { function providerTestDeleteLanguage() {
update_option($this->l->key, array( return array(
'languages' => array( array('de', true),
array('fr', false)
);
}
/**
* @dataProvider providerTestDeleteLanguage
*/
function testDeleteLanguage($code, $expected_result) {
$check = array(
'en' => array('name' => 'English'), 'en' => array('name' => 'English'),
'de' => array('name' => 'German') 'de' => array('name' => 'German')
) );
update_option($this->l->key, array(
'languages' => $check
)); ));
$this->l->delete_language('en'); $this->assertEquals(
$expected_result,
$this->l->delete_language($code)
);
if ($expected_result) {
unset($check[$code]);
}
$this->assertEquals(array( $this->assertEquals(array(
'languages' => array( 'languages' => $check ),
'de' => array('name' => 'German')
)
),
get_option($this->l->key) get_option($this->l->key)
); );
} }
function testAddLanguage() { function providerTestAddLanguage() {
return array(
array('de', 'German', true),
array('de', '', false),
array('', 'German', false),
array('en', 'English', false),
);
}
/**
* @dataProvider providerTestAddLanguage
*/
function testAddLanguage($code, $name, $expected_result) {
update_option($this->l->key, array( update_option($this->l->key, array(
'languages' => array( 'languages' => array(
'en' => array('name' => 'English') 'en' => array('name' => 'English')
) )
)); ));
$this->l->add_language('de', array('name' => 'German')); $this->assertEquals(
$expected_result,
$this->l->add_language($code, array('name' => $name))
);
$check = array(
'en' => array('name' => 'English')
);
if ($expected_result) {
$check[$code] = array('name' => $name);
}
$this->assertEquals(array( $this->assertEquals(array(
'languages' => array( 'languages' => $check
'en' => array('name' => 'English'),
'de' => array('name' => 'German')
)
), ),
get_option($this->l->key) get_option($this->l->key)
); );
@ -105,7 +141,7 @@ class WDTSLanguageOptionsTest extends PHPUnit_Framework_TestCase {
$this->assertEquals( $this->assertEquals(
$expected_result, $expected_result,
$result = $this->l->rename_language($code_to_rename, $new_name) $this->l->rename_language($code_to_rename, $new_name)
); );
$check = array( $check = array(