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'], '3'), 'Found 3 words to translate.'); + $this->assertTrue(substr_count($command['data'], 'value="Save suggestions"'), 'Found save button.'); + } + // Nothing to prepend... + else if (($command['method'] == 'prepend') && ($command['selector'] == NULL)) { + $this->assertTrue(empty($command['data']), 'No content will pe prepended.'); + } + else { + $this->assert(FALSE, 'Unknown method/selector combination.'); + debug($command); + } + + } + else { + $this->assert(FALSE, 'Unknown command.'); + debug($command); + } + } + + } + + + } diff --git a/tmgmt.api.php b/tmgmt.api.php index 1390658..40b44cf 100644 --- a/tmgmt.api.php +++ b/tmgmt.api.php @@ -38,12 +38,12 @@ function hook_tmgmt_source_plugin_info_alter(&$info) { } /** - * Return a list with TMGMTJobItem, title and a description. + * Return a list of suggested sources for job items. * * @param array $items - * An array with TMGMTJobItems which must be checked for suggested + * An array with TMGMTJobItem objects which must be checked for suggested * translations. - * - 0: TMGMTJobItem A JobItem to check for suggestions. + * - TMGMTJobItem A JobItem to check for suggestions. * - ... * @param TMGMTJob $job * The current translation job to check for additional translation items. @@ -51,16 +51,14 @@ function hook_tmgmt_source_plugin_info_alter(&$info) { * @return array * An array with all additional translation suggestions. * - job_item: A TMGMTJobItem instance. - * - suggested: A string which indicates where this suggestion comes from. - * - reference_link: Link to the entity this suggestions comes from. + * - referenced: A string which indicates where this suggestion comes from. * - from_job: The main TMGMTJob-ID which suggests this translation. */ function hook_tmgmt_source_suggestions(array $items, TMGMTJob $job) { return array( array( 'job_item' => tmgmt_job_item_create('entity', 'node', 0), - 'suggested' => t('Suggested @type entity', array('@type' => 'node')), - 'entity_uri' => entity_uri('node', 0), + 'referenced' => t('Referenced @type of field @label', array('@type' => 'entity', '@label' => 'label')), 'from_job' => $job->tjid, ) ); diff --git a/tmgmt.info b/tmgmt.info index 4a99f41..f581284 100644 --- a/tmgmt.info +++ b/tmgmt.info @@ -7,8 +7,6 @@ dependencies[] = entity dependencies[] = locale dependencies[] = views -test_dependencies[] = file_entity - files[] = includes/tmgmt.exception.inc files[] = controller/tmgmt.controller.job.inc files[] = controller/tmgmt.controller.job_item.inc @@ -37,7 +35,6 @@ files[] = tests/tmgmt.crud.test files[] = tests/tmgmt.plugin.test files[] = tests/tmgmt.helper.test files[] = tests/tmgmt.upgrade.alpha1.test -files[] = tests/tmgmt.suggestions.test ; Views integration and handlers files[] = views/tmgmt.views.inc diff --git a/ui/includes/tmgmt_ui.pages.inc b/ui/includes/tmgmt_ui.pages.inc index 9667890..8744547 100644 --- a/ui/includes/tmgmt_ui.pages.inc +++ b/ui/includes/tmgmt_ui.pages.inc @@ -425,7 +425,7 @@ function tmgmt_job_form($form, &$form_state, TMGMTJob $job, $op = 'edit') { '#header' => array(), '#options' => array(), '#multiple' => TRUE, - '#weight' => 20, + '#weight' => 30, ); // If this is an AJAX-Request, load all related nodes and fill the table. @@ -438,18 +438,12 @@ function tmgmt_job_form($form, &$form_state, TMGMTJob $job, $op = 'edit') { 'suggestions_save' => array( '#type' => 'submit', '#value' => t('Save suggestions'), - '#submit' => array('tmgmt_ui_ajax_submit_save_suggestions'), + '#submit' => array('tmgmt_ui_submit_save_suggestions'), '#limit_validation_errors' => array(array('suggestions_table')), '#attributes' => array( 'class' => array('tmgmt-ui-job-suggestions-save') ), - '#ajax' => array( - 'callback' => 'tmgmt_ui_ajax_callback_load_suggestions', - 'wrapper' => 'tmgmt-ui-job-items-suggestions', - 'method' => 'replace', - 'effect' => 'fade', - ), - '#weight' => 30, + '#weight' => 40, ), ); } @@ -457,6 +451,7 @@ function tmgmt_job_form($form, &$form_state, TMGMTJob $job, $op = 'edit') { '#type' => 'container', '#prefix' => '
', '#suffix' => '
', + '#weight' => 20, ) + $suggestions_table; } @@ -573,19 +568,14 @@ function tmgmt_ui_ajax_submit_load_suggestions(array $form, array &$form_state) /** * Saves selected suggestions as jobs and returns the outstanding suggestions. */ -function tmgmt_ui_ajax_submit_save_suggestions($form, &$form_state) { +function tmgmt_ui_submit_save_suggestions($form, &$form_state) { // Save all selected suggestion jobs. if (isset($form_state['values']['suggestions_table']) && is_array($form_state['values']['suggestions_table'])) { foreach ($form_state['values']['suggestions_table'] as $id) { - $id = check_plain($id); - if (isset($form_state['tmgmt_suggestions'][$id]['job_item']) - && isset($form_state['tmgmt_job']) - && ($form_state['tmgmt_suggestions'][$id]['job_item'] instanceof TMGMTJobItem) - && ($form_state['tmgmt_job'] instanceof TMGMTJob)) { - $item = $form_state['tmgmt_suggestions'][$id]['job_item']; - $form_state['tmgmt_job']->addJobItem($item); - // @todo: Maybe add the new item to the main 'job items table' and - // refresh that too. Or at least print a message to show success. + $key = (int)$id - 1; // Because in the tableselect we need an idx > 0. + if ($id && isset($form_state['tmgmt_suggestions'][$key]['job_item'])) { + $item = $form_state['tmgmt_suggestions'][$key]['job_item']; + $form_state['tmgmt_job']->addExistingItem($item); } } } @@ -627,11 +617,11 @@ function _tmgmt_ui_translation_suggestions(array &$suggestions_table, array &$fo $job->cleanSuggestionsList($form_state['tmgmt_suggestions']); // Process all valid entries. - foreach ($form_state['tmgmt_suggestions'] as $k => &$result) { + foreach ($form_state['tmgmt_suggestions'] as $k => $result) { if (is_array($result) && isset($result['job_item']) && ($result['job_item'] instanceof TMGMTJobItem)) { $entity = $result['job_item']; $suggested = isset($result['suggested']) ? $result['suggested'] : NULL; - $options[$k] = _tmgmt_ui_add_suggestion_job($entity, $suggested); + $options[$k + 1] = _tmgmt_ui_add_suggestion_job($entity, $suggested); } } @@ -656,7 +646,7 @@ function _tmgmt_ui_translation_suggestions(array &$suggestions_table, array &$fo * @return array * Options-Entry for a tableselect array. */ -function _tmgmt_ui_add_suggestion_job(TMGMTJobItem &$entity, $suggested = '') { +function _tmgmt_ui_add_suggestion_job(TMGMTJobItem $entity, $suggested = '') { $uri = $entity->getSourceUri(); return array( 'title' => $entity->label(),