From ac3bd9773efb44e4269db0f36365cc58757a154b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=82=E6=B5=81?= Date: Wed, 6 Mar 2019 17:24:36 +0000 Subject: [PATCH] Migrate transcriptions shell (script) to CakePHP3 `cake transcriptions script sentences` Refs #1784. --- src/Shell/TranscriptionsShell.php | 15 +++++++------- .../Shell/TranscriptionsShellTest.php | 20 +++++++++++++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/Shell/TranscriptionsShell.php b/src/Shell/TranscriptionsShell.php index 07a33bf693..1ca95e4b21 100644 --- a/src/Shell/TranscriptionsShell.php +++ b/src/Shell/TranscriptionsShell.php @@ -34,15 +34,14 @@ public function initialize() private function detectTranscriptionsFor($data) { $result = array(); foreach ($data as $row) { - $model = key($row); - $lang = isset($row[$model]['lang']) ? - $row[$model]['lang'] : - $row[$model]['sentence_lang']; - $text = $row[$model]['text']; - $script = $this->Transcription->detectScript($lang, $text); + $lang = isset($row->lang) ? + $row->lang : + $row->sentence_lang; + $text = $row->text; + $script = $this->Transcriptions->detectScript($lang, $text); $result[] = array( - 'id' => $row[$model]['id'], + 'id' => $row->id, 'script' => $script, 'modified' => false, ); @@ -122,7 +121,7 @@ protected function _setScript($rows, $model) { ); $entities = $this->{$model}->newEntities($data); if ($this->{$model}->saveMany($entities, $options)) - $proceeded += count($data); + $proceeded += count($entities); } $this->out('.', 0); return $proceeded; diff --git a/tests/TestCase/Shell/TranscriptionsShellTest.php b/tests/TestCase/Shell/TranscriptionsShellTest.php index c7a22a54fb..74723eaf98 100644 --- a/tests/TestCase/Shell/TranscriptionsShellTest.php +++ b/tests/TestCase/Shell/TranscriptionsShellTest.php @@ -23,6 +23,7 @@ private function getAutotranscriptionMock() { ->setMethods([ 'jpn_Jpan_to_Hrkt_generate', 'jpn_Jpan_to_Hrkt_validate', + 'cmn_detectScript', ]) ->getMock(); $AT->expects($this->any()) @@ -31,6 +32,10 @@ private function getAutotranscriptionMock() { $AT->expects($this->any()) ->method('jpn_Jpan_to_Hrkt_validate') ->will($this->returnValue(true)); + $AT->expects($this->any()) + ->method('cmn_detectScript') + ->will($this->returnValue('TEST')); + return $AT; } @@ -82,4 +87,19 @@ public function testAutogen_batched() $this->TS->batchOperationSize = 2; $this->testAutogen_forAllSentences(); } + + public function testSetSentencesScript() + { + $expectedScripts = ['TEST']; + + $this->TS->setSentencesScript('cmn'); + + $scripts = TableRegistry::get('Sentences') + ->find('list', ['valueField' => 'script']) + ->where(['lang' => 'cmn']) + ->toArray(); + + $scripts = array_keys(array_flip($scripts)); + $this->assertEquals($expectedScripts, $scripts); + } }