MessageGroupStatesUpdaterJobTest.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class MessageGroupStatesUpdaterJobTest extends MediaWikiTestCase {
00008     protected function setUp() {
00009         parent::setUp();
00010 
00011         global $wgHooks;
00012         $this->setMwGlobals( array(
00013             'wgHooks' => $wgHooks,
00014             'wgTranslateCC' => array(),
00015             'wgTranslateMessageIndex' => array( 'DatabaseMessageIndex' ),
00016             'wgTranslateWorkflowStates' => false,
00017             'wgTranslateGroupFiles' => array(),
00018             'wgTranslateTranslationServices' => array(),
00019         ) );
00020         $wgHooks['TranslatePostInitGroups'] = array( array( $this, 'getTestGroups' ) );
00021         MessageGroups::clearCache();
00022         MessageIndexRebuildJob::newJob()->run();
00023     }
00024 
00025     public function getTestGroups( &$list ) {
00026         $messages = array( 'key1' => 'msg1', 'key2' => 'msg2' );
00027         $list['group-trans'] = new MessageGroupWithTransitions( 'group-trans', $messages );
00028         $list['group-notrans'] = new MessageGroupWithoutTransitions( 'group-notrans', array() );
00029 
00030         return false;
00031     }
00032 
00033     public function testGetGroupsWithTransitions() {
00034         $handle = new MockMessageHandle();
00035         $groups = MessageGroupStatesUpdaterJob::getGroupsWithTransitions( $handle );
00036         foreach ( $groups as $id => $transitions ) {
00037             $this->assertEquals( 'group-trans', $id );
00038         }
00039     }
00040 
00044     public function testGetStatValue( $type, $expected ) {
00045         $stats = array(
00046             MessageGroupStats::TOTAL => 666,
00047             MessageGroupStats::FUZZY => 111,
00048             MessageGroupStats::TRANSLATED => 222,
00049             MessageGroupStats::PROOFREAD => 111,
00050         );
00051         $actual = MessageGroupStatesUpdaterJob::getStatValue( $stats, $type );
00052         $this->assertEquals( $expected, $actual );
00053     }
00054 
00055     public static function provideStatValues() {
00056         return array(
00057             array( 'UNTRANSLATED', 333 ),
00058             array( 'OUTDATED', 111 ),
00059             array( 'TRANSLATED', 222 ),
00060             array( 'PROOFREAD', 111 ),
00061         );
00062     }
00063 
00067     public function testMatchCondition( $expected, $value, $condition, $max ) {
00068         $actual = MessageGroupStatesUpdaterJob::matchCondition( $value, $condition, $max );
00069         $this->assertEquals( $expected, $actual );
00070     }
00071 
00072     public static function provideMatchCondition() {
00073         return array(
00074             array( true, 0, 'ZERO', 666 ),
00075             array( false, 1, 'ZERO', 666 ),
00076             array( true, 1, 'NONZERO', 666 ),
00077             array( false, 0, 'NONZERO', 666 ),
00078             array( true, 666, 'MAX', 666 ),
00079             array( false, 0, 'MAX', 666 ),
00080             array( false, 12, 'MAX', 666 ),
00081         );
00082     }
00083 
00084     public function testGetNewState() {
00085         $group = MessageGroups::getGroup( 'group-trans' );
00086         $transitions = $group->getMessageGroupStates()->getConditions();
00087 
00088         $stats = array( 5, 0, 0, 0 );
00089         $newstate = MessageGroupStatesUpdaterJob::getNewState( $stats, $transitions );
00090         $this->assertEquals( 'unset', $newstate, 'all zero, should be unset' );
00091 
00092         $stats = array( 5, 1, 0, 0 );
00093         $newstate = MessageGroupStatesUpdaterJob::getNewState( $stats, $transitions );
00094         $this->assertEquals( 'inprogress', $newstate, 'one translated message' );
00095 
00096         $stats = array( 5, 0, 1, 0 );
00097         $newstate = MessageGroupStatesUpdaterJob::getNewState( $stats, $transitions );
00098         $this->assertEquals( 'inprogress', $newstate, 'one outdated message' );
00099 
00100         $stats = array( 5, 1, 1, 0 );
00101         $newstate = MessageGroupStatesUpdaterJob::getNewState( $stats, $transitions );
00102         $this->assertEquals( 'inprogress', $newstate, 'one translated and one outdated message' );
00103 
00104         $stats = array( 5, 5, 0, 0 );
00105         $newstate = MessageGroupStatesUpdaterJob::getNewState( $stats, $transitions );
00106         $this->assertEquals( 'proofreading', $newstate, 'all translated' );
00107     }
00108 
00109     public function testHooks() {
00110         $user = new MockSuperUser();
00111         $group = MessageGroups::getGroup( 'group-trans' );
00112 
00113         // In the beginning...
00114         $currentState = ApiGroupReview::getState( $group, 'fi' );
00115         $this->assertEquals( false, $currentState, 'groups start from unset state' );
00116 
00117         // First translation
00118         $page = WikiPage::factory( Title::newFromText( 'MediaWiki:key1/fi' ) );
00119         $status = $page->doEdit( 'trans1', __METHOD__, 0, false, $user );
00120         self::runJobs();
00121         $currentState = ApiGroupReview::getState( $group, 'fi' );
00122         $this->assertEquals( 'inprogress', $currentState, 'in progress after first translation' );
00123 
00124         // First review
00125         ApiTranslationReview::doReview( $user, self::getRevision( $status ), __METHOD__ );
00126         self::runJobs();
00127         $currentState = ApiGroupReview::getState( $group, 'fi' );
00128         $this->assertEquals( 'inprogress', $currentState, 'in progress while untranslated messages' );
00129 
00130         // Second translation
00131         $page = WikiPage::factory( Title::newFromText( 'MediaWiki:key2/fi' ) );
00132         $status = $page->doEdit( 'trans2', __METHOD__, 0, false, $user );
00133         self::runJobs();
00134         $currentState = ApiGroupReview::getState( $group, 'fi' );
00135         $this->assertEquals( 'proofreading', $currentState, 'proofreading after second translation' );
00136 
00137         // Second review
00138         ApiTranslationReview::doReview( $user, self::getRevision( $status ), __METHOD__ );
00139         self::runJobs();
00140         $currentState = ApiGroupReview::getState( $group, 'fi' );
00141         $this->assertEquals( 'ready', $currentState, 'ready when all proofread' );
00142 
00143         // Change to translation
00144         $page = WikiPage::factory( Title::newFromText( 'MediaWiki:key1/fi' ) );
00145         $page->doEdit( 'trans1 updated', __METHOD__, 0, false, $user );
00146         self::runJobs();
00147         $currentState = ApiGroupReview::getState( $group, 'fi' );
00148         $this->assertEquals(
00149             'proofreading',
00150             $currentState,
00151             'back to proofreading after translation changed'
00152         );
00153     }
00154 
00155     protected static function getRevision( Status $s ) {
00156         $value = $s->getValue();
00157 
00158         return $value['revision'];
00159     }
00160 
00161     protected static function runJobs() {
00162         do {
00163             $job = Job::pop();
00164             if ( !$job ) {
00165                 break;
00166             }
00167             $job->run();
00168         } while ( true );
00169     }
00170 }
00171 
00172 class MockMessageHandle extends MessageHandle {
00173     public function __construct() {
00174     }
00175 
00176     public function getGroupIds() {
00177         return array( 'group-trans', 'group-notrans' );
00178     }
00179 }
00180 
00181 class MessageGroupWithoutTransitions extends MockWikiMessageGroup {
00182     public function getMessageGroupStates() {
00183         return new MessageGroupStates();
00184     }
00185 }
00186 
00187 class MessageGroupWithTransitions extends MockWikiMessageGroup {
00188     public function getMessageGroupStates() {
00189         return new MessageGroupStates( array(
00190             'state conditions' => array(
00191                 array( 'ready', array( 'PROOFREAD' => 'MAX' ) ),
00192                 array( 'proofreading', array( 'TRANSLATED' => 'MAX' ) ),
00193                 array(
00194                     'unset',
00195                     array(
00196                         'UNTRANSLATED' => 'MAX',
00197                         'OUTDATED' => 'ZERO',
00198                         'TRANSLATED' => 'ZERO'
00199                     )
00200                 ),
00201                 array( 'inprogress', array( 'UNTRANSLATED' => 'NONZERO' ) ),
00202             )
00203         ) );
00204     }
00205 }
Generated on Tue Oct 29 00:00:24 2013 for MediaWiki Translate Extension by  doxygen 1.6.3