diff --git a/controller/tmgmt.controller.job_item.inc b/controller/tmgmt.controller.job_item.inc index d0052da..5dfd454 100644 --- a/controller/tmgmt.controller.job_item.inc +++ b/controller/tmgmt.controller.job_item.inc @@ -18,7 +18,7 @@ class TMGMTJobItemController extends EntityAPIController { */ public function save($entity, DatabaseTransaction $transaction = NULL) { $entity->changed = REQUEST_TIME; - $entity->recalculateWords(); + $entity->recalculateStatistics(); return parent::save($entity, $transaction); } diff --git a/entity/tmgmt.entity.job.inc b/entity/tmgmt.entity.job.inc index a685bba..398baac 100644 --- a/entity/tmgmt.entity.job.inc +++ b/entity/tmgmt.entity.job.inc @@ -202,7 +202,7 @@ class TMGMTJob extends Entity { * @param TMGMTJobItem $job * The job item to add. */ - function addJobItem(TMGMTJobItem &$item) { + function addExistingItem(TMGMTJobItem &$item) { $item->tjid = $this->tjid; $item->save(); } @@ -785,19 +785,20 @@ class TMGMTJob extends Entity { * Conditions to pass only some and not all items to the hook. * * @return array - * List with all translation suggestions. + * An array with all additional translation suggestions. + * - job_item: A TMGMTJobItem instance. + * - referenced: A string which indicates where this suggestion comes from. + * - from_job: The main TMGMTJob-ID which suggests this translation. */ public function getSuggestions(array $conditions = array()) { $suggestions = module_invoke_all('tmgmt_source_suggestions', $this->getItems($conditions), $this); // Each TMGMTJob needs a job id to be able to count the words, because the // source-language is stored in the job and not the item. - foreach ($suggestions as &$job) { - $jobItem = &$job['job_item']; - if (!isset($jobItem->tjid)) { - $jobItem->tjid = $this->tjid; - $jobItem->recalculateWords(); - } + foreach ($suggestions as &$suggestion) { + $jobItem = $suggestion['job_item']; + $jobItem->tjid = $this->tjid; + $jobItem->recalculateStatistics(); } return $suggestions; } @@ -814,7 +815,7 @@ class TMGMTJob extends Entity { * - tmgmt_job: An instance of a TMGMTJobItem. */ public function cleanSuggestionsList(array &$suggestions) { - foreach ($suggestions as $k => &$suggestion) { + foreach ($suggestions as $k => $suggestion) { if (is_array($suggestion) && isset($suggestion['job_item']) && ($suggestion['job_item'] instanceof TMGMTJobItem)) { $jobItem = $suggestion['job_item']; diff --git a/entity/tmgmt.entity.job_item.inc b/entity/tmgmt.entity.job_item.inc index 8b6c705..8c55b82 100644 --- a/entity/tmgmt.entity.job_item.inc +++ b/entity/tmgmt.entity.job_item.inc @@ -289,7 +289,7 @@ class TMGMTJobItem extends Entity { if ($controller = $this->getSourceController()) { return $controller->getData($this); } - return FALSE; + return array(); } /** @@ -739,9 +739,9 @@ class TMGMTJobItem extends Entity { } /** - * Recount all translatable words. + * Recalculate statistical word-data: pending, translated, reviewed, accepted. */ - public function recalculateWords() { + public function recalculateStatistics() { // Set translatable data from the current entity to calculate words. if (empty($this->data)) { $this->data = $this->getSourceData(); diff --git a/sources/entity/tmgmt_entity.info b/sources/entity/tmgmt_entity.info index 260f741..c99359d 100644 --- a/sources/entity/tmgmt_entity.info +++ b/sources/entity/tmgmt_entity.info @@ -2,10 +2,17 @@ name = Entity Source description = Entity source plugin for the Translation Management system. package = Translation Management core = 7.x + +dependencies[] = entity dependencies[] = tmgmt dependencies[] = tmgmt_field + test_dependencies[] = pathauto +test_dependencies[] = file_entity + +files[] = ../tests/tmgmt.base.test files[] = tmgmt_entity.source.test files[] = tmgmt_entity.pathauto.test +files[] = tmgmt_entity.suggestions.test files[] = tmgmt_entity.plugin.inc files[] = tmgmt_entity.ui.inc diff --git a/sources/entity/tmgmt_entity.module b/sources/entity/tmgmt_entity.module index 77b3cb1..ea9e81b 100644 --- a/sources/entity/tmgmt_entity.module +++ b/sources/entity/tmgmt_entity.module @@ -232,7 +232,7 @@ function tmgmt_entity_tmgmt_source_suggestions(array $items, TMGMTJob $job) { case 'image': // 'File' (and images) must be translatable entity types. // Other files we not suggest here. Get all field items from the - // current entity and suggest them as translatable. + // current field and suggest them as translatable. $field_name = $field['field_name']; if (isset($entity_types['file']) && ($field_items = field_get_items($item->item_type, $entity, $field_name))) { // Add all files as a suggestion. @@ -252,7 +252,7 @@ function tmgmt_entity_tmgmt_source_suggestions(array $items, TMGMTJob $job) { // Add the translation as a suggestion. $suggestions[] = array( 'job_item' => tmgmt_job_item_create('entity', 'file', $file_entity->fid), - 'suggested' => t('Suggested @type entity', array('@type' => $field_type)), + 'referenced' => t('Referenced @type of field @label', array('@type' => $field_type, '@label' => $instance['label'])), 'from_job' => $item->tjid, ); } diff --git a/tests/tmgmt.suggestions.test b/sources/entity/tmgmt_entity.suggestions.test similarity index 53% rename from tests/tmgmt.suggestions.test rename to sources/entity/tmgmt_entity.suggestions.test index 3200889..6b2a6a9 100644 --- a/tests/tmgmt.suggestions.test +++ b/sources/entity/tmgmt_entity.suggestions.test @@ -9,8 +9,17 @@ */ class TMGMTSuggestionsTestCase extends TMGMTBaseTestCase { + static function getInfo() { + return array( + 'name' => t('Suggestions tests'), + 'description' => t('Basic suggestion operations for jobs and job-items'), + 'group' => t('Translation Management'), + 'dependencies' => array('file_entity'), + ); + } + public function setUp() { - parent::setUp(array('file_entity', 'tmgmt_entity', 'entity_translation')); + parent::setUp(array('file_entity', 'tmgmt_entity', 'entity_translation', 'tmgmt_ui')); $this->loginAsAdmin(array('administer entity translation')); $this->setEnvironment('de'); @@ -23,22 +32,18 @@ class TMGMTSuggestionsTestCase extends TMGMTBaseTestCase { } /** - * Implements getInfo(). - */ - static function getInfo() { - return array( - 'name' => t('Suggestions tests'), - 'description' => t('Basic suggestion operations for jobs and job-items'), - 'group' => t('Translation Management'), - 'dependencies' => array('file_entity'), - ); - } - - /** - * Test suggested entities from a translation job. + * Prepare a node to get suggestions from. + * + * Creates a node with two file fields. The first one is not translatable, + * the second one is. Both fields got two files attached, where one has + * translatable content (title and atl-text) and the other one not. + * + * @return object + * The node which is prepared with all needed fields for the suggestions. */ - function testSuggestions() { - // Create a content type with fields; only the first field is translatable. + protected function prepareTranslationSuggestions() { + // Create a content type with fields. + // Only the first field is a translatable reference. $type = $this->drupalCreateContentType(); $field1 = field_create_field(array( @@ -119,7 +124,7 @@ class TMGMTSuggestionsTestCase extends TMGMTBaseTestCase { 'value' => $this->randomName(), ), )), - 'field1' => array(LANGUAGE_NONE => array( + $field1['field_name'] => array(LANGUAGE_NONE => array( array( 'fid' => $file1->fid, 'display' => 1, @@ -131,7 +136,7 @@ class TMGMTSuggestionsTestCase extends TMGMTBaseTestCase { 'description' => '', ), )), - 'field2' => array('en' => array( + $field2['field_name'] => array(LANGUAGE_NONE => array( array( 'fid' => $file3->fid, 'display' => 1, @@ -144,25 +149,93 @@ class TMGMTSuggestionsTestCase extends TMGMTBaseTestCase { ), )), )); + return $node; + } - - // Create a job and get all suggestions. + /** + * Test suggested entities from a translation job. + */ + public function testSuggestions() { + // Prepare a job and a node for testing. $job = $this->createJob(); + $node = $this->prepareTranslationSuggestions(); $item = $job->addItem('entity', 'node', $node->nid); + + // Get all suggestions and clean the list. $suggestions = $job->getSuggestions(); $job->cleanSuggestionsList($suggestions); // Check for one suggestion. - $this->assertEqual(count($suggestions), 1, t('Found one suggestion.')); + $this->assertEqual(count($suggestions), 1, 'Found one suggestion.'); - // Add the suggestion to the job and re-get all suggestions. + // Check for valid attributes on the job item. $suggestion = reset($suggestions); - $job->addJobItem($suggestion['job_item']); + $this->assertEqual($suggestion['job_item']->getWordCount(), 3, 'Three translatable words in the suggestion.'); + $this->assertEqual($suggestion['job_item']->plugin, 'entity', 'Got an entity as plugin in the suggestion.'); + $this->assertEqual($suggestion['job_item']->item_type, 'file', 'Got a file in the suggestion.'); + $this->assertEqual($suggestion['job_item']->item_id, $node->field1[LANGUAGE_NONE][1]['fid'], 'File id match between node and suggestion.'); + + // Add the suggestion to the job and re-get all suggestions. + $job->addExistingItem($suggestion['job_item']); $suggestions = $job->getSuggestions(); $job->cleanSuggestionsList($suggestions); // Check for no more suggestions. - $this->assertEqual(count($suggestions), 0, t('Found no more suggestion.')); + $this->assertEqual(count($suggestions), 0, 'Found no more suggestion.'); } + /** + * Frontend-Test to check the loading and adding functions. + */ + public function testLoadSuggestions() { + // Prepare a job and a node for testing. + $job = $this->createJob(); + $node = $this->prepareTranslationSuggestions(); + $item = $job->addItem('entity', 'node', $node->nid); + + // Go to the job details page. + $this->drupalGet('admin/config/regional/tmgmt/jobs/' . $job->tjid); + + // Load all suggestions. + $commands = $this->drupalPostAJAX(NULL, array(), array('op' => t('Load suggestions'))); + $this->assertEqual(count($commands), 4, 'Found 4 commands in AJAX-Request.'); + + // Check each command for success. + foreach ($commands as $command) { + // No checks against the settings because we not use ajax to save. + if ($command['command'] == 'settings') { + } + // Other commands must be from type "insert". + else if ($command['command'] == 'insert') { + // This should be the tableselect javascript file for the header. + if (($command['method'] == 'prepend') && ($command['selector'] == 'head')) { + $this->assertTrue(substr_count($command['data'], 'misc/tableselect.js'), 'Javascript for Tableselect found.'); + } + // Check for the main content, the tableselect with the suggestions. + else if (($command['method'] == NULL) && ($command['selector'] == NULL)) { + $this->assertTrue(substr_count($command['data'], '') == 4, 'Found four table header.'); + $this->assertTrue(substr_count($command['data'], '') == 2, 'Found one suggestion and one table header.'); + $this->assertTrue(substr_count($command['data'], '