Xataface Translation Memory Module 0.1
Translation Memory for Xataface Applications
/Applications/XAMPP/xamppfiles/htdocs/recipedb/modules/tm/tests/test_XFTranslationMemory.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  * Xataface Translation Memory Module
00004  * Copyright (C) 2011  Steve Hannah <steve@weblite.ca>
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Library General Public
00017  * License along with this library; if not, write to the
00018  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
00019  * Boston, MA  02110-1301, USA.
00020  *
00021  */
00022 import('PHPUnit.php');
00023 import(dirname(__FILE__).'/../lib/XFTranslationMemory.php');
00024 class modules_tm_XFTranslationMemoryTest extends PHPUnit_TestCase {
00025 
00026         private $mod = null;
00027         
00028         function modules_calendar_RepeatEventTest( $name = 'modules_tm_XFTranslationMemoryTest'){
00029                 $this->PHPUnit_TestCase($name);
00030                 
00031         }
00032 
00033         function setUp(){
00034                 $base = 'xf_tm_';
00035                 $tables = array(
00036                         $base.'records',
00037                         $base.'record_strings',
00038                         $base.'strings',
00039                         $base.'translations',
00040                         $base.'translations_comments',
00041                         $base.'translations_score',
00042                         $base.'translations_status',
00043                         $base.'translation_memories',
00044                         $base.'translation_memories_managers',
00045                         $base.'translation_memory_translations',
00046                         $base.'translation_statuses',
00047                         $base.'workflows',
00048                         $base.'workflow_records',
00049                         $base.'workflow_steps',
00050                         $base.'workflow_step_changes',
00051                         $base.'workflow_step_panels',
00052                         $base.'workflow_step_panel_actions',
00053                         $base.'workflow_step_panel_members',
00054                         $base.'workflow_strings'
00055                         
00056                 
00057                 );
00058                 
00059                 foreach ($tables as $table){
00060                         self::q("delete from `".$table."`");
00061                 }
00062                 
00063         }
00064         
00065         
00066         
00067         
00068         
00069         function tearDown(){
00070                 
00071                 
00072 
00073         }
00074         
00075         
00076         function testTranslationMemory(){
00077                 $mod = Dataface_ModuleTool::loadModule('modules_tm');
00078                 
00079                 // Create a translation memory from thin air.
00080                 $rec = new Dataface_Record('xf_tm_translation_memories', array());
00081                 $rec->setValues(array(
00082                         'translation_memory_name'=>"Test",
00083                         'source_language'=>'en',
00084                         'destination_language'=>'es'
00085                 ));
00086                 
00087                 $tm = new XFTranslationMemory($rec);
00088                 
00089                 $this->assertEquals('en', $tm->getSourceLanguage());
00090                 $this->assertEquals('es', $tm->getDestinationLanguage());
00091                 $this->assertEquals($rec, $tm->getRecord());
00092                 
00093                 $dtm = XFTranslationMemory::getDefaultTranslationMemory('en','es');
00094                 $this->assertTrue($dtm instanceof XFTranslationMemory);
00095                 
00096                 $res = self::q("select count(*) from xf_tm_translation_memories");
00097                 list($count) = mysql_fetch_row($res);
00098                 @mysql_free_result($res);
00099                 $this->assertEquals(1, $count, "Should only be one translation memory: the default one we just inserted.");
00100                 
00101                 
00102                 $this->assertEquals("en", $dtm->getSourceLanguage());
00103                 $this->assertEquals("es", $dtm->getDestinationLanguage());
00104                 
00105                 $res = self::q("select count(*) from xf_tm_records");
00106                 list($count) = mysql_fetch_row($res);
00107                 @mysql_free_result($res);
00108                 $this->assertEquals(1, $count, "Should be one row in records table: the default one we just added.");
00109                 $row = df_get_record('xf_tm_records', array('translation_memory_id'=>'='.$dtm->getRecord()->val('translation_memory_id')));
00110                 $this->assertTrue($row instanceof Dataface_Record);
00111                 $this->assertEquals('*', $row->val('record_id'));
00112                 
00113                 
00114                 $dtm2 = XFTranslationMemory::getDefaultTranslationMemory('en','es');
00115                 $this->assertEquals($dtm, $dtm2, 'Default translation memory should be cached so should always return the same object for the same pair.');
00116                 
00117                 
00118                 $str = $dtm->addString('Test string', 'en');
00119                 
00120                 $this->assertTrue($str instanceof Dataface_Record);
00121                 $this->assertEquals('xf_tm_strings', $str->table()->tablename);
00122                 $this->assertEquals('Test string', $str->val('string_value'));
00123                 $this->assertEquals('Test string', $str->val('normalized_value'));
00124                 $this->assertEquals(md5('Test string'), $str->val('hash'));
00125                 
00126                 $str2 = df_get_record('xf_tm_strings', array('string_id'=>'='.$str->val('string_id')));
00127                 $this->assertTrue($str2 instanceof Dataface_Record);
00128                 $this->assertEquals('Test string', $str2->val('string_value'));
00129                 $this->assertEquals($str->val('string_id'), $str2->val('string_id'));
00130                 
00131                 $str3 = XFTranslationMemory::findString('Test string', 'en');
00132                 $this->assertTrue($str3 instanceof Dataface_Record);
00133                 $this->assertEquals($str->val('string_id'), $str3->val('string_id'));
00134                 
00135                 $str4 = XFTranslationMemory::findString('Test string', 'es');
00136                 $this->assertEquals(null, $str4, "String exists in english but not spanish so should return null.");
00137                 
00138                 
00139                 $tr = $dtm->findTranslation('Test string', 'String teste');
00140                 $this->assertEquals(null, $tr);
00141                 
00142                 $tr = $dtm->addTranslation('Test string', 'String teste', 'shannah');
00143                 $this->assertTrue($tr instanceof Dataface_Record);
00144                 $this->assertEquals('xf_tm_translations', $tr->table()->tablename);
00145                 $this->assertEquals($str->val('string_id'), $tr->val('string_id'));
00146                 $this->assertEquals('String teste', $tr->val('translation_value'));
00147                 $this->assertEquals('String teste', $tr->val('normalized_translation_value'));
00148                 $this->assertEquals(md5('String teste'), $tr->val('translation_hash'));
00149                 
00150                 
00151                 
00152                 // Now make sure we can't add the same string twice
00153                 $str5 = XFTranslationMemory::addString('Test string', 'en');
00154                 $this->assertEquals($str->val('string_id'), $str5->val('string_id'));
00155                 
00156                 
00157                 // Try adding a translation using string id only
00158                 $tr2 = $dtm->addTranslation($str->val('string_id'), 'String teste2', 'shannah');
00159                 $this->assertEquals($str->val('string_id'), $tr2->val('string_id'));
00160                 $this->assertEquals('String teste2', $tr2->val('translation_value'));
00161                 
00162                 $trf = $dtm->findTranslation('Test string', 'String teste');
00163                 $this->assertEquals($trf->val('translation_id'), $tr->val('translation_id'));
00164                 
00165                 $trf2 = $dtm->findTranslation($str->val('string_id'), 'String teste');
00166                 $this->assertEquals($trf2->val('translation_id'), $tr->val('translation_id'));
00167                 $this->assertEquals($trf2->val('string_id'), $str->val('string_id'));
00168                 
00169                 // Try adding the same translation
00170                 //echo "About to add our troubled string";
00171                 $tr3 = $dtm->addTranslation('Test string', 'String teste', 'shannah');
00172                 $this->assertEquals($tr->val('translation_id'), $tr3->val('translation_id'));
00173                 
00174                 // Make sure strings are case sensitive
00175                 $tr4 = $dtm->addTranslation('Test String', 'String Teste', 'shannah');
00176                 $this->assertTrue($tr4->val('translation_id') != $tr->val('translation_id'));
00177                 $this->assertTrue($tr4->val('string_id') != $tr->val('string_id'));
00178                 
00179                 
00180                 $this->assertTrue($dtm->containsTranslation('Test String', 'String Teste'));
00181                 $this->assertTrue($dtm->containsTranslation('Test string', 'String teste'));
00182                 $this->assertTrue(!$dtm->containsTranslation('Test2', '2Teste'));
00183                 $this->assertTrue(!$dtm->containsTranslation('Test String', 'String teste'));
00184                 $this->assertTrue(!$dtm->containsTranslation('Test string', 'foo'));
00185                 
00186                 $sources = array('foo','bar');
00187                 $translations = $dtm->getTranslations($sources);
00188                 $this->assertEquals(
00189                         array(
00190                                 0 => null,
00191                                 1 => null
00192                         ),
00193                         $translations
00194                 );
00195                 
00196                 $sources = array('Test String','Test string');
00197                 $translations = $dtm->getTranslations($sources);
00198                 $this->assertEquals(
00199                         array(
00200                                 0 => null,
00201                                 1 => null
00202                         ),
00203                         $translations
00204                 );
00205                 
00206                 $dtm->setTranslationStatus('Test String', 'String Teste', XFTranslationMemory::TRANSLATION_APPROVED, 'shannah');
00207                 $translations = $dtm->getTranslations($sources);
00208                 $this->assertEquals(
00209                         array(
00210                                 0 => 'String Teste',
00211                                 1 => null
00212                         ),
00213                         $translations
00214                 );
00215                 
00216                 $translations = $dtm->getTranslations($sources, 3,4); // Only get the submitted but not the approved
00217                 $this->assertEquals(
00218                         array(
00219                                 0 => null,
00220                                 1 => null
00221                         ),
00222                         $translations
00223                 );
00224                 sleep(1); // Necessary so that the approval statuses get different timestamps
00225                 $dtm->setTranslationStatus('Test String', 'String Teste', XFTranslationMemory::TRANSLATION_SUBMITTED, 'shannah');
00226                 $translations = $dtm->getTranslations($sources, 5); // Only get the submitted but not the approved
00227                 $this->assertEquals(
00228                         array(
00229                                 0 => null,
00230                                 1 => null
00231                         ),
00232                         $translations
00233                 );
00234                 
00235                 
00236                 
00237                 
00238                 
00239                 
00240                 
00241                 
00242         
00243                 
00244                 
00245         }
00246         
00247         
00248         static function q($sql){
00249                 $res = mysql_query($sql, df_db());
00250                 if ( !$res ) throw new Exception(mysql_error(df_db()));
00251                 return $res;
00252         }
00253         
00254                 
00255 
00256 
00257 }
00258 
00259 
00260 // Add this test to the suite of tests to be run by the testrunner
00261 Dataface_ModuleTool::getInstance()->loadModule('modules_testrunner')
00262                 ->addTest('modules_tm_XFTranslationMemoryTest');
 All Data Structures Files Functions Variables