/** * Creates a job item and adds it into the cart. * * @param string $plugin * The source plugin. * @param string $item_type * The source item type. * @param $item_id * The source item id. * * @return JobItem|null * Added job item. If the item exists NULL is returned. */ public function addJobItem($plugin, $item_type, $item_id) { if ($this->isSourceItemAdded($plugin, $item_type, $item_id)) { return NULL; } $job_item = tmgmt_job_item_create($plugin, $item_type, $item_id); $job_item->save(); $this->cart[] = $job_item->id(); return $job_item; }
/** * Test if the source is able to pull content in requested language. */ function testCartEnforceSourceLanguage() { $content_type = $this->drupalCreateContentType(); $node_sk = $this->drupalCreateNode(array('title' => $this->randomMachineName(), 'langcode' => 'sk', 'type' => $content_type->id())); $node = $node_sk->addTranslation('en'); $node->title->value = $this->randomMachineName(); $node->body->value = $this->randomMachineName(); $node->save(); $node_cs = $this->drupalCreateNode(array('title' => $this->randomMachineName(), 'langcode' => 'cs', 'type' => $content_type->id())); $this->loginAsTranslator(); $job_item_sk = tmgmt_job_item_create('content', 'node', $node_sk->id()); $job_item_sk->save(); $this->drupalGet('tmgmt-add-to-cart/' . $job_item_sk->id()); $job_items_data[$job_item_sk->getItemId()] = $job_item_sk->getItemType(); $job_item_cs = tmgmt_job_item_create('content', 'node', $node_cs->id()); $job_item_cs->save(); $this->drupalGet('tmgmt-add-to-cart/' . $job_item_cs->id()); $job_items_data[$job_item_cs->getItemId()] = $job_item_cs->getItemType(); $this->drupalPostForm('admin/tmgmt/cart', array('enforced_source_language' => TRUE, 'source_language' => 'en', 'target_language[]' => array('es')), t('Request translation')); $this->assertText(t('One job needs to be checked out.')); $this->assertRaw(t('One item skipped as for the language @language it was not possible to retrieve a translation.', array('@language' => 'English'))); $this->assertText(t('You have enforced the job source language which most likely resulted in having a translation of your original content as the job source text. You should review the job translation received from the translator carefully to prevent the content quality loss.')); $args = explode('/', $this->getUrl()); $tjid = array_pop($args); $this->drupalPostForm(NULL, array(), t('Submit to provider')); // We cannot test for the item data as items without a job are not able to // get the data in case the source language is overridden. Therefore only // testing for item_id and item_type values. foreach (Job::load($tjid)->getItems() as $job_item) { $this->assertEqual($job_items_data[$job_item->getItemId()], $job_item->getItemType()); } $this->drupalGet('admin/tmgmt/cart'); $this->assertText($node_cs->getTitle()); $this->assertNoText($node_sk->getTitle()); // Test that duplicate submission of an item is not allowed. $this->drupalPostForm('admin/tmgmt/cart', array('target_language[]' => array('es')), t('Request translation')); $this->drupalPostForm(NULL, array(), t('Submit to provider')); $this->assertText(t('Test translation created.')); $job_item_cs = tmgmt_job_item_create('content', 'node', $node_cs->id()); $job_item_cs->save(); $this->drupalGet('tmgmt-add-to-cart/' . $job_item_cs->id()); $job_items_data[$job_item_cs->getItemId()] = $job_item_cs->getItemType(); $this->drupalPostForm('admin/tmgmt/cart', array('target_language[]' => array('es')), t('Request translation')); $this->assertText(t('1 item conflict with pending item and will be dropped on submission.')); $this->drupalPostForm(NULL, array(), t('Submit to provider')); $this->assertText(t('All job items are conflicting, the job can not be submitted.')); }
/** * Tests that the referenced entities are always embedded. */ public function testTranslatableEntityReferences() { // Assert there is NO embedded references yet. $this->drupalGet('/admin/tmgmt/settings'); $xpath = '//*[@id="edit-embedded-fields"]'; $embedded_entity = '<label for="edit-embedded-fields-always-embedded">Always embedded</label>'; $embedded_node = '<span class="fieldset-legend">Content</span>'; $this->assertFalse(strpos($this->xpath($xpath)[0]->asXml(), $embedded_entity)); $this->assertFalse(strpos($this->xpath($xpath)[0]->asXml(), $embedded_node)); // Create the reference field to the composite entity test. $field_storage = FieldStorageConfig::create(array('field_name' => 'entity_test_composite', 'entity_type' => 'node', 'type' => 'entity_reference', 'settings' => array('target_type' => 'entity_test_composite'))); $field_storage->save(); $field = FieldConfig::create(array('field_storage' => $field_storage, 'bundle' => 'article', 'translatable' => FALSE)); $field->save(); // Assert there IS the entity_test_composite as entity embedded now. $this->drupalGet('/admin/tmgmt/settings'); $this->assertTrue(strpos($this->xpath($xpath)[0]->asXml(), $embedded_entity)); $this->assertFalse(strpos($this->xpath($xpath)[0]->asXml(), $embedded_node)); // Create the composite entity test. $composite = EntityTestComposite::create(array('name' => 'composite name')); $composite->save(); // Create a node with a reference to the composite entity test. $node = $this->createNode(array('title' => 'node title', 'type' => 'article', 'entity_test_composite' => $composite)); // Create a job and job item for the node. $job = $this->createJob(); $job->save(); $job_item = tmgmt_job_item_create('content', $node->getEntityTypeId(), $node->id(), ['tjid' => $job->id()]); $job_item->save(); // Get the data and check it contains the data for the composite entity. $data = $job_item->getData(); $this->assertTrue(isset($data['entity_test_composite'])); $this->assertEqual($data['entity_test_composite']['#label'], 'entity_test_composite'); $this->assertFalse(isset($data['entity_test_composite'][0]['#label'])); $this->assertEqual($data['entity_test_composite'][0]['entity']['name']['#label'], 'Name'); $this->assertEqual($data['entity_test_composite'][0]['entity']['name'][0]['value']['#text'], 'composite name'); // Ensure that only Content is shown in the source select form. $this->drupalGet('/admin/tmgmt/sources'); $this->assertOption('edit-source', 'content:node'); $this->assertNoOption('edit-source', 'content:entity_test_composite'); }
/** * Test the preview of TMGMT local. */ public function testPreview() { // Create translatable node. $this->createNodeType('article', 'Article', TRUE); $node = $this->createTranslatableNode('article', 'en'); $node->setPublished(FALSE); $node->save(); $translator = Translator::load('local'); $job = $this->createJob('en', 'de'); $job->translator = $translator; $job->save(); $job_item = tmgmt_job_item_create('content', $node->getEntityTypeId(), $node->id(), array('tjid' => $job->id())); $job_item->save(); // Create another local translator with the required abilities. $this->loginAsAdmin($this->localManagerPermissions); // Configure language abilities. $edit = array('tmgmt_translation_skills[0][language_from]' => 'en', 'tmgmt_translation_skills[0][language_to]' => 'de'); $this->drupalPostForm('user/' . $this->admin_user->id() . '/edit', $edit, t('Save')); $this->drupalGet('admin/tmgmt/jobs/' . $job->id()); $edit = ['settings[translator]' => $this->admin_user->id()]; $this->drupalPostForm(NULL, $edit, t('Submit to provider')); $this->drupalGet('translate'); $this->clickLink('View'); $this->clickLink('Translate'); // Check preview. $edit = array('title|0|value[translation]' => $translation1 = 'German translation of title', 'body|0|value[translation][value]' => $translation2 = 'German translation of body'); $this->drupalPostForm(NULL, $edit, t('Preview')); $this->assertResponse(200); $this->assertText($translation1); $this->assertText($translation2); $this->drupalGet('translate'); $this->clickLink('View'); $this->clickLink('Translate'); // Assert source link $this->assertLink($node->getTitle()); // Test that local translator can access an unpublished node. $this->clickLink($node->getTitle()); $this->assertText($node->getTitle()); }
/** * Test content entity source preview. */ public function testSourceUpdate() { // Create translatable node. $node = $this->createTranslatableNode('article', 'en'); $job = $this->createJob('en', 'de'); $job->save(); $job_item = tmgmt_job_item_create('content', $node->getEntityTypeId(), $node->id(), array('tjid' => $job->id())); $job_item->save(); $updated_body = 'New body'; $edit = ['body[0][value]' => $updated_body]; $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, 'Save'); $this->drupalGet('admin/tmgmt/items/' . $job_item->id()); $this->assertText($updated_body, 'Source updated correctly.'); }
/** * Return a list of suggested sources for job items. * * @param array $items * An array with TMGMTJobItem objects which must be checked for suggested * translations. * - TMGMTJobItem A JobItem to check for suggestions. * - ... * @param TMGMTJob $job * The current translation job to check for additional translation items. * * @return array * 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. */ function hook_tmgmt_source_suggestions(array $items, TMGMTJob $job) { return array(array('job_item' => tmgmt_job_item_create('entity', 'node', 0), 'reason' => t('Referenced @type of field @label', array('@type' => 'entity', '@label' => 'label')), 'from_item' => $items[1]->tjiid)); }
/** * Tests the user config entity. */ public function testAccountSettings() { $this->installConfig(['user']); $this->config('user.settings')->set('anonymous', 'Test Anonymous')->save(); $job = tmgmt_job_create('en', 'de'); $job->translator = 'test_translator'; $job->save(); $job_item = tmgmt_job_item_create('config', '_simple_config', 'entity.user.admin_form', array('tjid' => $job->id())); $job_item->save(); $source_plugin = $this->container->get('plugin.manager.tmgmt.source')->createInstance('config'); $data = $source_plugin->getData($job_item); // Test the name property. $this->assertEqual($data['user__settings']['anonymous']['#label'], 'Name'); $this->assertEqual($data['user__settings']['anonymous']['#text'], 'Test Anonymous'); $this->assertEqual($data['user__settings']['anonymous']['#translate'], TRUE); // Test item types. $this->assertEqual($source_plugin->getItemTypes()['view'], t('View')); // Now request a translation and save it back. $job->requestTranslation(); $items = $job->getItems(); $item = reset($items); $item->acceptTranslation(); $data = $item->getData(); // Check that the translations were saved correctly. $language_manager = \Drupal::languageManager(); $language_manager->setConfigOverrideLanguage($language_manager->getLanguage('de')); $this->assertEqual(\Drupal::config('user.settings')->get('anonymous'), $data['user__settings']['anonymous']['#translation']['#text']); }
/** * Test extraction and saving translation for embedded references. */ public function testEmbeddedReferences() { $field1 = FieldStorageConfig::create(array('field_name' => 'field1', 'entity_type' => $this->entityTypeId, 'type' => 'entity_reference', 'cardinality' => -1, 'settings' => array('target_type' => $this->entityTypeId))); $field1->save(); $field2 = FieldStorageConfig::create(array('field_name' => 'field2', 'entity_type' => $this->entityTypeId, 'type' => 'entity_reference', 'cardinality' => -1, 'settings' => array('target_type' => $this->entityTypeId))); $field2->save(); // Create field instances on the content type. FieldConfig::create(array('field_storage' => $field1, 'bundle' => $this->entityTypeId, 'label' => 'Field 1', 'translatable' => FALSE, 'settings' => array()))->save(); FieldConfig::create(array('field_storage' => $field2, 'bundle' => $this->entityTypeId, 'label' => 'Field 2', 'translatable' => FALSE, 'settings' => array()))->save(); // Create a test entity that can be referenced. $referenced_values = ['langcode' => 'en', 'user_id' => 1, 'name' => $this->randomString()]; $this->config('tmgmt_content.settings')->set('embedded_fields.' . $this->entityTypeId . '.field1', TRUE)->save(); $referenced_entity = entity_create($this->entityTypeId, $referenced_values); $referenced_entity->save(); // Create an english test entity. $values = array('langcode' => 'en', 'user_id' => 1); $entity_test = entity_create($this->entityTypeId, $values); $translation = $entity_test->getTranslation('en'); $translation->name->value = $this->randomMachineName(); $translation->field1->target_id = $referenced_entity->id(); $translation->field2->target_id = $referenced_entity->id(); $entity_test->save(); $job = tmgmt_job_create('en', 'de'); $job->translator = 'test_translator'; $job->save(); $job_item = tmgmt_job_item_create('content', $this->entityTypeId, $entity_test->id(), array('tjid' => $job->id())); $job_item->save(); $source_plugin = $this->container->get('plugin.manager.tmgmt.source')->createInstance('content'); $data = $source_plugin->getData($job_item); // Ensure that field 2 is not in the extracted data. $this->assertFalse(isset($data['field2'])); // Ensure some labels and structure for field 1. $this->assertEqual($data['field1']['#label'], 'Field 1'); $this->assertFalse(isset($data['field1'][0]['#label'])); $this->assertEqual($data['field1'][0]['entity']['name']['#label'], 'Name'); $this->assertEqual($data['field1'][0]['entity']['name'][0]['value']['#text'], $referenced_values['name']); // Now request a translation and save it back. $job->requestTranslation(); $items = $job->getItems(); $item = reset($items); $item->acceptTranslation(); $data = $item->getData(); // Check that the translations were saved correctly. $entity_test = entity_load($this->entityTypeId, $entity_test->id()); $translation = $entity_test->getTranslation('de'); $referenced_entity = entity_load($this->entityTypeId, $referenced_entity->id()); $referenced_translation = $referenced_entity->getTranslation('de'); $this->assertEqual($referenced_translation->name->value, $data['field1'][0]['entity']['name'][0]['value']['#translation']['#text']); }
/** * {@inheritdoc} */ public function addItem($plugin, $item_type, $item_id) { $transaction = db_transaction(); $is_new = FALSE; if ($this->isNew()) { $this->save(); $is_new = TRUE; } $item = tmgmt_job_item_create($plugin, $item_type, $item_id, array('tjid' => $this->id())); $item->save(); if ($item->getWordCount() == 0) { $transaction->rollback(); // In case we got word count 0 for the first job item, NULL tjid so that // if there is another addItem() call the rolled back job object will get // persisted. if ($is_new) { $this->tjid = NULL; } throw new TMGMTException('Job item @label (@type) has no translatable content.', array('@label' => $item->label(), '@type' => $item->getSourceType())); } return $item; }
/** * Test the cart functionality. */ function testCart() { $this->addLanguage('fr'); $job_items = array(); // Create a few job items and add them to the cart. for ($i = 1; $i < 6; $i++) { $job_item = tmgmt_job_item_create('test_source', 'test', $i); $job_item->save(); $job_items[$i] = $job_item; } $this->loginAsTranslator(); foreach ($job_items as $job_item) { $this->drupalGet('tmgmt-add-to-cart/' . $job_item->id()); } // Check if the items are displayed in the cart. $this->drupalGet('admin/tmgmt/cart'); foreach ($job_items as $job_item) { $this->assertText($job_item->label()); } // Test the remove items from cart functionality. $this->drupalPostForm(NULL, ['items[1]' => TRUE, 'items[2]' => FALSE, 'items[3]' => FALSE, 'items[4]' => TRUE, 'items[5]' => FALSE], t('Remove selected')); $this->assertText($job_items[2]->label()); $this->assertText($job_items[3]->label()); $this->assertText($job_items[5]->label()); $this->assertNoText($job_items[1]->label()); $this->assertNoText($job_items[4]->label()); $this->assertText(t('Job items were removed from the cart.')); // Test that removed job items from cart were deleted as well. $existing_items = JobItem::loadMultiple(); $this->assertTrue(!isset($existing_items[$job_items[1]->id()])); $this->assertTrue(!isset($existing_items[$job_items[4]->id()])); $this->drupalPostForm(NULL, array(), t('Empty cart')); $this->assertNoText($job_items[2]->label()); $this->assertNoText($job_items[3]->label()); $this->assertNoText($job_items[5]->label()); $this->assertText(t('All job items were removed from the cart.')); // No remaining job items. $existing_items = JobItem::loadMultiple(); $this->assertTrue(empty($existing_items)); $language_sequence = array('en', 'en', 'fr', 'fr', 'de', 'de'); for ($i = 1; $i < 7; $i++) { $job_item = tmgmt_job_item_create('test_source', 'test', $i); $job_item->save(); $job_items[$i] = $job_item; $languages[$job_items[$i]->id()] = $language_sequence[$i - 1]; } \Drupal::state()->set('tmgmt.test_source_languages', $languages); foreach ($job_items as $job_item) { $this->drupalGet('tmgmt-add-to-cart/' . $job_item->id()); } $this->drupalPostForm('admin/tmgmt/cart', array('items[' . $job_items[1]->id() . ']' => TRUE, 'items[' . $job_items[2]->id() . ']' => TRUE, 'items[' . $job_items[3]->id() . ']' => TRUE, 'items[' . $job_items[4]->id() . ']' => TRUE, 'items[' . $job_items[5]->id() . ']' => TRUE, 'items[' . $job_items[6]->id() . ']' => FALSE, 'target_language[]' => array('en', 'de')), t('Request translation')); $this->assertText(t('@count jobs need to be checked out.', array('@count' => 4))); // We should have four jobs with following language combinations: // [fr, fr] => [en] // [de] => [en] // [en, en] => [de] // [fr, fr] => [de] $jobs = entity_load_multiple_by_properties('tmgmt_job', array('source_language' => 'fr', 'target_language' => 'en')); $job = reset($jobs); $this->assertEqual(count($job->getItems()), 2); $jobs = entity_load_multiple_by_properties('tmgmt_job', array('source_language' => 'de', 'target_language' => 'en')); $job = reset($jobs); $this->assertEqual(count($job->getItems()), 1); $jobs = entity_load_multiple_by_properties('tmgmt_job', array('source_language' => 'en', 'target_language' => 'de')); $job = reset($jobs); $this->assertEqual(count($job->getItems()), 2); $jobs = entity_load_multiple_by_properties('tmgmt_job', array('source_language' => 'fr', 'target_language' => 'de')); $job = reset($jobs); $this->assertEqual(count($job->getItems()), 2); $this->drupalGet('admin/tmgmt/cart'); // Both fr and one de items must be gone. $this->assertNoText($job_items[1]->label()); $this->assertNoText($job_items[2]->label()); $this->assertNoText($job_items[3]->label()); $this->assertNoText($job_items[4]->label()); $this->assertNoText($job_items[5]->label()); // One de item is in the cart as it was not selected for checkout. $this->assertText($job_items[6]->label()); // Check to see if no items are selected and the error message pops up. $this->drupalPostForm('admin/tmgmt/cart', ['items[' . $job_items[6]->id() . ']' => FALSE], t('Request translation')); $this->assertUniqueText(t("You didn't select any source items.")); }
/** * Tests of the job item review process. */ public function testReviewForm() { // Create the field body with multiple delta. $field_storage = FieldStorageConfig::create(array('field_name' => 'body_test', 'entity_type' => 'node', 'type' => 'text', 'cardinality' => -1, 'translatable' => TRUE)); $field_storage->save(); FieldConfig::create(array('field_storage' => $field_storage, 'bundle' => 'test_bundle'))->save(); // Create the field image with multiple value and delta. $field_storage = FieldStorageConfig::create(array('field_name' => 'image_test_multi', 'entity_type' => 'node', 'type' => 'image', 'cardinality' => -1, 'translatable' => TRUE)); $field_storage->save(); FieldConfig::create(array('field_storage' => $field_storage, 'bundle' => 'test_bundle'))->save(); // Create the field image with multiple value and delta. $field_storage = FieldStorageConfig::create(array('field_name' => 'image_test_single', 'entity_type' => 'node', 'type' => 'image', 'cardinality' => 1, 'translatable' => TRUE)); $field_storage->save(); FieldConfig::create(array('field_storage' => $field_storage, 'bundle' => 'test_bundle'))->save(); // Create two images. $image1 = array('target_id' => $this->image->id(), 'alt' => $this->randomMachineName(), 'title' => $this->randomMachineName()); $image2 = array('target_id' => $this->image->id(), 'alt' => $this->randomMachineName(), 'title' => $this->randomMachineName()); // Create the node. $settings = array('title' => $this->randomMachineName(), 'type' => 'test_bundle', 'body_test' => array($this->randomMachineName(), $this->randomMachineName()), 'image_test_single' => $image1, 'image_test_multi' => array($image1, $image2)); $node = Node::create($settings); $node->save(); // Create a Job with the node. $job = tmgmt_job_create('en', 'de'); $job->translator = 'test_translator'; $job->save(); $job_item = tmgmt_job_item_create('content', 'node', $node->id(), array('tjid' => $job->id())); $job_item->save(); // Access to the review form. $this->drupalGet('admin/tmgmt/items/1'); // Test that all the items are being displayed. $this->assertRaw('name="title|0|value[source]"'); $this->assertRaw('name="body_test|0|value[source]"'); $this->assertRaw('name="body_test|1|value[source]"'); $this->assertRaw('name="image_test_multi|0|title[source]"'); $this->assertRaw('name="image_test_multi|0|alt[source]"'); $this->assertRaw('name="image_test_multi|1|title[source]"'); $this->assertRaw('name="image_test_multi|1|alt[source]"'); $this->assertRaw('name="image_test_single|0|title[source]"'); $this->assertRaw('name="image_test_single|0|alt[source]"'); // Check the labels for the title. $this->assertEqual($this->xpath('//*[@id="tmgmt-ui-element-title-wrapper"]/table/tbody/tr[1]/th'), NULL); $this->assertEqual($this->xpath('//*[@id="tmgmt-ui-element-title-wrapper"]/table/tbody/tr[2]/td[1]/div[1]/label'), NULL); // Check the labels for the multi delta body. $delta = $this->xpath('//*[@id="tmgmt-ui-element-body-test-wrapper"]/table/tbody/tr[1]/td[1]/div[1]/label'); $this->assertEqual($delta[0], 'Delta #0'); $delta = $this->xpath('//*[@id="tmgmt-ui-element-body-test-wrapper"]/table/tbody[2]/tr[1]/td[1]/div[1]/label'); $this->assertEqual($delta[0], 'Delta #1'); // Check the labels for the multi delta/multi value image. $delta = $this->xpath('//*[@id="tmgmt-ui-element-image-test-multi-wrapper"]/table/tbody[1]/tr[1]/th'); $this->assertEqual($delta[0], 'Delta #0'); $label = $this->xpath('//*[@id="tmgmt-ui-element-image-test-multi-wrapper"]/table/tbody[1]/tr[2]/td[1]/div[1]/label'); $this->assertEqual($label[0], 'Alternative text'); $label = $this->xpath('//*[@id="tmgmt-ui-element-image-test-multi-wrapper"]/table/tbody[1]/tr[4]/td[1]/div[1]/label'); $this->assertEqual($label[0], 'Title'); $delta = $this->xpath('//*[@id="tmgmt-ui-element-image-test-multi-wrapper"]/table/tbody[2]/tr[1]/th'); $this->assertEqual($delta[0], 'Delta #1'); $label = $this->xpath('//*[@id="tmgmt-ui-element-image-test-multi-wrapper"]/table/tbody[2]/tr[2]/td[1]/div[1]/label'); $this->assertEqual($label[0], 'Alternative text'); $label = $this->xpath('//*[@id="tmgmt-ui-element-image-test-multi-wrapper"]/table/tbody[2]/tr[4]/td[1]/div[1]/label'); $this->assertEqual($label[0], 'Title'); // Check the labels for the multi value image. $this->assertEqual($this->xpath('//*[@id="tmgmt-ui-element-image-test-single-wrapper"]/table/tbody/tr[1]/th'), NULL); $label = $this->xpath('//*[@id="tmgmt-ui-element-image-test-single-wrapper"]/table/tbody/tr[1]/td[1]/div[1]/label'); $this->assertEqual($label[0], 'Alternative text'); $label = $this->xpath('//*[@id="tmgmt-ui-element-image-test-single-wrapper"]/table/tbody/tr[3]/td[1]/div[1]/label'); $this->assertEqual($label[0], 'Title'); }