tmgmt_entity.suggestions.test 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /*
  3. * @file
  4. * Contains tests for Translation management
  5. */
  6. /**
  7. * Basic Source-Suggestions tests.
  8. */
  9. class TMGMTSuggestionsTestCase extends TMGMTBaseTestCase {
  10. static function getInfo() {
  11. return array(
  12. 'name' => 'Entity Suggestions tests',
  13. 'description' => 'Tests suggestion implementation for the entity source plugin',
  14. 'group' => 'Translation Management',
  15. 'dependencies' => array(
  16. 'file_entity',
  17. 'entityreference',
  18. ),
  19. );
  20. }
  21. public function setUp() {
  22. parent::setUp(array('file_entity', 'tmgmt_entity', 'tmgmt_ui', 'entityreference', 'tmgmt_i18n_string', 'i18n_menu'));
  23. $this->loginAsAdmin(array('administer entity translation'));
  24. $this->setEnvironment('de');
  25. // Enable entity translations for nodes and comments.
  26. $edit = array();
  27. $edit['entity_translation_entity_types[node]'] = 1;
  28. $edit['entity_translation_entity_types[file]'] = 1;
  29. $this->drupalPost('admin/config/regional/entity_translation', $edit, t('Save configuration'));
  30. }
  31. /**
  32. * Prepare a node to get suggestions from.
  33. *
  34. * Creates a node with two file fields. The first one is not translatable,
  35. * the second one is. Both fields got two files attached, where one has
  36. * translatable content (title and atl-text) and the other one not.
  37. *
  38. * @return object
  39. * The node which is prepared with all needed fields for the suggestions.
  40. */
  41. protected function prepareTranslationSuggestions() {
  42. // Create a content type with fields.
  43. // Only the first field is a translatable reference.
  44. $type = $this->drupalCreateContentType();
  45. $field1 = field_create_field(array(
  46. 'field_name' => 'field1',
  47. 'type' => 'file',
  48. 'cardinality' => -1,
  49. ));
  50. $field2 = field_create_field(array(
  51. 'field_name' => 'field2',
  52. 'type' => 'file',
  53. 'cardinality' => -1,
  54. 'translatable' => TRUE,
  55. ));
  56. $field3 = field_create_field(array(
  57. 'field_name' => 'field3',
  58. 'type' => 'entityreference',
  59. 'cardinality' => -1,
  60. 'settings' => array(
  61. 'target_type' => 'node',
  62. 'handler' => 'base',
  63. 'handler_settings' => array(
  64. 'target_bundles' => array($type->type => $type->type),
  65. 'sort' => array('type' => 'none'),
  66. ),
  67. ),
  68. ));
  69. // Create field instances on the content type.
  70. field_create_instance(array(
  71. 'field_name' => $field1['field_name'],
  72. 'entity_type' => 'node',
  73. 'bundle' => $type->type,
  74. 'label' => 'Field 1',
  75. 'widget' => array('type' => 'file'),
  76. 'settings' => array(),
  77. ));
  78. field_create_instance(array(
  79. 'field_name' => $field2['field_name'],
  80. 'entity_type' => 'node',
  81. 'bundle' => $type->type,
  82. 'label' => 'Field 2',
  83. 'widget' => array('type' => 'file'),
  84. 'settings' => array(),
  85. ));
  86. field_create_instance(array(
  87. 'field_name' => $field3['field_name'],
  88. 'entity_type' => 'node',
  89. 'bundle' => $type->type,
  90. 'label' => 'Field 3',
  91. 'settings' => array(),
  92. 'widget' => array('type' => 'entityreference_autocomplete_tags'),
  93. ));
  94. // Make the body field translatable from node.
  95. $info = field_info_field('body');
  96. $info['translatable'] = TRUE;
  97. field_update_field($info);
  98. // Make the file entity fields translatable.
  99. $info = field_info_field('field_file_image_alt_text');
  100. $info['translatable'] = TRUE;
  101. field_update_field($info);
  102. $info = field_info_field('field_file_image_title_text');
  103. $info['translatable'] = TRUE;
  104. field_update_field($info);
  105. // Create and save files - two with some text and two with no text.
  106. list($file1, $file2, $file3, $file4) = $this->drupalGetTestFiles('image');
  107. $file2->field_file_image_alt_text['en'][0] = array(
  108. 'value' => $this->randomName(),
  109. 'type' => 'plain_text',
  110. );
  111. $file2->field_file_image_title_text['en'][0] = array(
  112. 'value' => $this->randomName() . ' ' . $this->randomName(),
  113. 'type' => 'plain_text',
  114. );
  115. $file4->field_file_image_alt_text['en'][0] = array(
  116. 'value' => $this->randomName(),
  117. 'type' => 'plain_text',
  118. );
  119. $file4->field_file_image_title_text['en'][0] = array(
  120. 'value' => $this->randomName() . ' ' . $this->randomName(),
  121. 'type' => 'plain_text',
  122. );
  123. file_save($file1);
  124. file_save($file2);
  125. file_save($file3);
  126. file_save($file4);
  127. // Create a dummy node that will be referenced
  128. $referenced_node = $this->drupalCreateNode(array(
  129. 'type' => $type->type,
  130. 'language' => 'en',
  131. 'body' => array(
  132. 'en' => array(
  133. array('value' => $this->randomName() . ' ' . $this->randomName()),
  134. ),
  135. ),
  136. ));
  137. // Create a node with two translatable and two non-translatable files.
  138. $node = $this->drupalCreateNode(array(
  139. 'type' => $type->type,
  140. 'language' => 'en',
  141. 'body' => array('en' => array(
  142. array(
  143. 'value' => $this->randomName(),
  144. ),
  145. )),
  146. $field1['field_name'] => array(LANGUAGE_NONE => array(
  147. array(
  148. 'fid' => $file1->fid,
  149. 'display' => 1,
  150. 'description' => '',
  151. ),
  152. array(
  153. 'fid' => $file2->fid,
  154. 'display' => 1,
  155. 'description' => '',
  156. ),
  157. )),
  158. $field2['field_name'] => array(LANGUAGE_NONE => array(
  159. array(
  160. 'fid' => $file3->fid,
  161. 'display' => 1,
  162. 'description' => '',
  163. ),
  164. array(
  165. 'fid' => $file4->fid,
  166. 'display' => 1,
  167. 'description' => '',
  168. ),
  169. )),
  170. $field3['field_name'] => array(LANGUAGE_NONE => array(
  171. array('target_id' => $referenced_node->nid),
  172. )),
  173. ));
  174. // Create a translatable menu.
  175. $config = array(
  176. 'menu_name' => 'translatable-menu',
  177. 'title' => 'Translatable menu',
  178. 'description' => $this->randomName(),
  179. 'i18n_mode' => I18N_MODE_MULTIPLE,
  180. );
  181. menu_save($config);
  182. $menu = menu_load($config['menu_name']);
  183. // Create a menu link for the node.
  184. $menu_link = array(
  185. 'link_path' => 'node/' . $node->nid,
  186. 'link_title' => 'Menu link one',
  187. // i18n_menu_link::get_title() uses the title, set that too.
  188. 'title' => 'Menu link one',
  189. 'menu_name' => $menu['menu_name'],
  190. 'customized' => TRUE,
  191. );
  192. $node->link = menu_link_load(menu_link_save($menu_link));
  193. return $node;
  194. }
  195. /**
  196. * Test suggested entities from a translation job.
  197. */
  198. public function testSuggestions() {
  199. // Prepare a job and a node for testing.
  200. $job = $this->createJob();
  201. $node = $this->prepareTranslationSuggestions();
  202. $item = $job->addItem('entity', 'node', $node->nid);
  203. // Get all suggestions and clean the list.
  204. $suggestions = $job->getSuggestions();
  205. $job->cleanSuggestionsList($suggestions);
  206. // Check for suggestions.
  207. $this->assertEqual(count($suggestions), 4, 'Found four suggestions.');
  208. // Check for valid attributes on the suggestions.
  209. foreach ($suggestions as $suggestion) {
  210. switch ($suggestion['reason']) {
  211. case 'Field Field 1':
  212. $this->assertEqual($suggestion['job_item']->getWordCount(), 3, 'Three translatable words in the suggestion.');
  213. $this->assertEqual($suggestion['job_item']->plugin, 'entity', 'Got an entity as plugin in the suggestion.');
  214. $this->assertEqual($suggestion['job_item']->item_type, 'file', 'Got a file in the suggestion.');
  215. $this->assertEqual($suggestion['job_item']->item_id, $node->field1[LANGUAGE_NONE][1]['fid'], 'File id match between node and suggestion.');
  216. break;
  217. case 'Field Field 2':
  218. $this->assertEqual($suggestion['job_item']->getWordCount(), 3, 'Three translatable words in the suggestion.');
  219. $this->assertEqual($suggestion['job_item']->plugin, 'entity', 'Got an entity as plugin in the suggestion.');
  220. $this->assertEqual($suggestion['job_item']->item_type, 'file', 'Got a file in the suggestion.');
  221. $this->assertEqual($suggestion['job_item']->item_id, $node->field2[LANGUAGE_NONE][1]['fid'], 'File id match between node and suggestion.');
  222. break;
  223. case 'Field Field 3':
  224. $this->assertEqual($suggestion['job_item']->getWordCount(), 2, 'Two translatable words in the suggestion');
  225. $this->assertEqual($suggestion['job_item']->plugin, 'entity', 'Got an entity as plugin in the suggestion.');
  226. $this->assertEqual($suggestion['job_item']->item_type, 'node', 'Got a node in the suggestion.');
  227. $this->assertEqual($suggestion['job_item']->item_id, $node->field3[LANGUAGE_NONE][0]['target_id'], 'File id match between node and suggestion.');
  228. break;
  229. case 'Menu link Menu link one':
  230. $this->assertEqual($suggestion['job_item']->getWordCount(), 3, 'Three translatable words in the suggestion' . $suggestion['job_item']->plugin . $suggestion['job_item']->item_type);
  231. $this->assertEqual($suggestion['job_item']->plugin, 'i18n_string', 'Got a string as plugin in the suggestion.');
  232. $this->assertEqual($suggestion['job_item']->item_type, 'menu_link', 'Got a menu link in the suggestion.');
  233. $this->assertEqual($suggestion['job_item']->item_id, 'menu:item:'. $node->link['mlid'], 'Menu link id match between menu link and suggestion.');
  234. break;
  235. default:
  236. $this->fail('Found an invalid suggestion.');
  237. break;
  238. }
  239. $this->assertEqual($suggestion['from_item'], $item->tjiid);
  240. $job->addExistingItem($suggestion['job_item']);
  241. }
  242. // Re-get all suggestions.
  243. $suggestions = $job->getSuggestions();
  244. $job->cleanSuggestionsList($suggestions);
  245. // Check for no more suggestions.
  246. $this->assertEqual(count($suggestions), 0, 'Found no more suggestion.');
  247. }
  248. }