field_collection.test 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. <?php
  2. /**
  3. * @file
  4. * field_collections tests.
  5. */
  6. /**
  7. * Test basics.
  8. */
  9. class FieldCollectionBasicTestCase extends DrupalWebTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Field collection',
  13. 'description' => 'Tests creating and using field collections.',
  14. 'group' => 'Field types',
  15. );
  16. }
  17. function setUp() {
  18. parent::setUp('field_collection');
  19. // Create a field_collection field to use for the tests.
  20. $this->field_name = 'field_test_collection';
  21. $this->field = array('field_name' => $this->field_name, 'type' => 'field_collection', 'cardinality' => 4);
  22. $this->field = field_create_field($this->field);
  23. $this->field_id = $this->field['id'];
  24. $this->instance = array(
  25. 'field_name' => $this->field_name,
  26. 'entity_type' => 'node',
  27. 'bundle' => 'article',
  28. 'label' => $this->randomName() . '_label',
  29. 'description' => $this->randomName() . '_description',
  30. 'weight' => mt_rand(0, 127),
  31. 'settings' => array(),
  32. 'widget' => array(
  33. 'type' => 'hidden',
  34. 'label' => 'Test',
  35. 'settings' => array(),
  36. ),
  37. );
  38. $this->instance = field_create_instance($this->instance);
  39. }
  40. /**
  41. * Helper for creating a new node with a field collection item.
  42. */
  43. protected function createNodeWithFieldCollection() {
  44. $node = $this->drupalCreateNode(array('type' => 'article'));
  45. // Manually create a field_collection.
  46. $entity = entity_create('field_collection_item', array('field_name' => $this->field_name));
  47. $entity->setHostEntity('node', $node);
  48. $entity->save();
  49. return array($node, $entity);
  50. }
  51. /**
  52. * Tests CRUD.
  53. */
  54. function testCRUD() {
  55. list ($node, $entity) = $this->createNodeWithFieldCollection();
  56. $node = node_load($node->nid, NULL, TRUE);
  57. $this->assertEqual($entity->item_id, $node->{$this->field_name}[LANGUAGE_NONE][0]['value'], 'A field_collection has been successfully created and referenced.');
  58. $this->assertEqual($entity->revision_id, $node->{$this->field_name}[LANGUAGE_NONE][0]['revision_id'], 'A field_collection has been successfully created and referenced.');
  59. // Test adding an additional field_collection during node edit.
  60. $entity2 = entity_create('field_collection_item', array('field_name' => $this->field_name));
  61. $node->{$this->field_name}[LANGUAGE_NONE][] = array('entity' => $entity2);
  62. node_save($node);
  63. $node = node_load($node->nid, NULL, TRUE);
  64. $this->assertTrue(!empty($entity2->item_id) && !empty($entity2->revision_id), 'Field_collection has been saved.');
  65. $this->assertEqual($entity->item_id, $node->{$this->field_name}[LANGUAGE_NONE][0]['value'], 'Existing reference has been kept during update.');
  66. $this->assertEqual($entity->revision_id, $node->{$this->field_name}[LANGUAGE_NONE][0]['revision_id'], 'Existing reference has been kept during update (revision).');
  67. $this->assertEqual($entity2->item_id, $node->{$this->field_name}[LANGUAGE_NONE][1]['value'], 'New field_collection has been properly referenced');
  68. $this->assertEqual($entity2->revision_id, $node->{$this->field_name}[LANGUAGE_NONE][1]['revision_id'], 'New field_collection has been properly referenced (revision)');
  69. // Make sure deleting the field_collection removes the reference.
  70. $entity2->delete();
  71. $node = node_load($node->nid, NULL, TRUE);
  72. $this->assertTrue(!isset($node->{$this->field_name}[LANGUAGE_NONE][1]), 'Reference correctly deleted.');
  73. // Make sure field_collections are removed during deletion of the host.
  74. node_delete($node->nid);
  75. $this->assertTrue(entity_load('field_collection_item', FALSE) === array(), 'Field collections are deleted when the host is deleted.');
  76. // Try deleting nodes with collections without any values.
  77. $node = $this->drupalCreateNode(array('type' => 'article'));
  78. node_delete($node->nid);
  79. $this->assertTrue(node_load($node->nid, NULL, TRUE) == FALSE, 'Node without collection values deleted.');
  80. // Test creating a field collection entity with a not-yet saved host entity.
  81. $node = entity_create('node', array('type' => 'article'));
  82. $entity = entity_create('field_collection_item', array('field_name' => $this->field_name));
  83. $entity->setHostEntity('node', $node);
  84. $entity->save();
  85. // Now the node should have been saved with the collection and the link
  86. // should have been established.
  87. $this->assertTrue(!empty($node->nid), 'Node has been saved with the collection.');
  88. $this->assertTrue(count($node->{$this->field_name}[LANGUAGE_NONE]) == 1 && !empty($node->{$this->field_name}[LANGUAGE_NONE][0]['value']) && !empty($node->{$this->field_name}[LANGUAGE_NONE][0]['revision_id']), 'Link has been established.');
  89. // Again, test creating a field collection with a not-yet saved host entity,
  90. // but this time save both entities via the host.
  91. $node = entity_create('node', array('type' => 'article'));
  92. $entity = entity_create('field_collection_item', array('field_name' => $this->field_name));
  93. $entity->setHostEntity('node', $node);
  94. node_save($node);
  95. $this->assertTrue(!empty($entity->item_id) && !empty($entity->revision_id), 'Collection has been saved with the host.');
  96. $this->assertTrue(count($node->{$this->field_name}[LANGUAGE_NONE]) == 1 && !empty($node->{$this->field_name}[LANGUAGE_NONE][0]['value']) && !empty($node->{$this->field_name}[LANGUAGE_NONE][0]['revision_id']), 'Link has been established.');
  97. // Test Revisions.
  98. list ($node, $item) = $this->createNodeWithFieldCollection();
  99. $entity2 = entity_create('field_collection_item', array('field_name' => $this->field_name));
  100. $node->{$this->field_name}[LANGUAGE_NONE][] = array('entity' => $entity2);
  101. node_save($node);
  102. $this->assertEqual($entity2->archived, FALSE, 'New field collection item with new content revision is not archived.');
  103. // Test saving a new revision of a node.
  104. $node->revision = TRUE;
  105. node_save($node);
  106. $item_updated = field_collection_item_load($node->{$this->field_name}[LANGUAGE_NONE][0]['value']);
  107. $this->assertNotEqual($item->revision_id, $item_updated->revision_id, 'Creating a new host entity revision creates a new field collection revision.');
  108. // Test saving a new revision with a new field collection item.
  109. $node->revision = TRUE;
  110. // Test saving the node without creating a new revision.
  111. $item = $item_updated;
  112. $node->revision = FALSE;
  113. node_save($node);
  114. $item_updated = field_collection_item_load($node->{$this->field_name}[LANGUAGE_NONE][0]['value']);
  115. $this->assertEqual($item->revision_id, $item_updated->revision_id, 'Updating a new host entity without creating a new revision does not create a new field collection revision.');
  116. // Create a new revision of the node, such we have a non default node and
  117. // field collection revision. Then test using it.
  118. $vid = $node->vid;
  119. $item_revision_id = $item_updated->revision_id;
  120. $node->revision = TRUE;
  121. node_save($node);
  122. $item_updated = field_collection_item_load($node->{$this->field_name}[LANGUAGE_NONE][0]['value']);
  123. $this->assertNotEqual($item_revision_id, $item_updated->revision_id, 'Creating a new host entity revision creates a new field collection revision.');
  124. $this->assertTrue($item_updated->isDefaultRevision(), 'Field collection of default host entity revision is default too.');
  125. $this->assertEqual($item_updated->hostEntityId(), $node->nid, 'Can access host entity ID of default field collection revision.');
  126. $this->assertEqual($item_updated->hostEntity()->vid, $node->vid, 'Loaded default host entity revision.');
  127. $item = entity_revision_load('field_collection_item', $item_revision_id);
  128. $this->assertFalse($item->isDefaultRevision(), 'Field collection of non-default host entity is non-default too.');
  129. $this->assertEqual($item->hostEntityId(), $node->nid, 'Can access host entity ID of non-default field collection revision.');
  130. $this->assertEqual($item->hostEntity()->vid, $vid, 'Loaded non-default host entity revision.');
  131. // Delete the non-default revision and make sure the field collection item
  132. // revision has been deleted too.
  133. entity_revision_delete('node', $vid);
  134. $this->assertFalse(entity_revision_load('node', $vid), 'Host entity revision deleted.');
  135. $this->assertFalse(entity_revision_load('field_collection_item', $item_revision_id), 'Field collection item revision deleted.');
  136. // Test having archived field collections, i.e. collections referenced only
  137. // in non-default revisions.
  138. list ($node, $item) = $this->createNodeWithFieldCollection();
  139. // Create two revisions.
  140. $node_vid = $node->vid;
  141. $node->revision = TRUE;
  142. node_save($node);
  143. $node_vid2 = $node->vid;
  144. $node->revision = TRUE;
  145. node_save($node);
  146. // Now delete the field collection item for the default revision.
  147. $item = field_collection_item_load($node->{$this->field_name}[LANGUAGE_NONE][0]['value']);
  148. $item_revision_id = $item->revision_id;
  149. $item->deleteRevision();
  150. $node = node_load($node->nid);
  151. $this->assertTrue(!isset($node->{$this->field_name}[LANGUAGE_NONE][0]), 'Field collection item revision removed from host.');
  152. $this->assertFalse(field_collection_item_revision_load($item->revision_id), 'Field collection item default revision deleted.');
  153. $item = field_collection_item_load($item->item_id);
  154. $this->assertNotEqual($item->revision_id, $item_revision_id, 'Field collection default revision has been updated.');
  155. $this->assertTrue($item->archived, 'Field collection item has been archived.');
  156. $this->assertFalse($item->isInUse(), 'Field collection item specified as not in use.');
  157. $this->assertTrue($item->isDefaultRevision(), 'Field collection of non-default host entity is default (but archived).');
  158. $this->assertEqual($item->hostEntityId(), $node->nid, 'Can access host entity ID of non-default field collection revision.');
  159. $this->assertEqual($item->hostEntity()->nid, $node->nid, 'Loaded non-default host entity revision.');
  160. // Test deleting a revision of an archived field collection.
  161. $node_revision2 = node_load($node->nid, $node_vid2);
  162. $item = field_collection_item_revision_load($node_revision2->{$this->field_name}[LANGUAGE_NONE][0]['revision_id']);
  163. $item->deleteRevision();
  164. // There should be one revision left, so the item should still exist.
  165. $item = field_collection_item_load($item->item_id);
  166. $this->assertTrue($item->archived, 'Field collection item is still archived.');
  167. $this->assertFalse($item->isInUse(), 'Field collection item specified as not in use.');
  168. // Test that deleting the first node revision deletes the whole field
  169. // collection item as it contains its last revision.
  170. node_revision_delete($node_vid);
  171. $this->assertFalse(field_collection_item_load($item->item_id), 'Archived field collection deleted when last revision deleted.');
  172. // Test that removing a field-collection item also deletes it.
  173. list ($node, $item) = $this->createNodeWithFieldCollection();
  174. $node->{$this->field_name}[LANGUAGE_NONE] = array();
  175. $node->revision = FALSE;
  176. node_save($node);
  177. $this->assertFalse(field_collection_item_load($item->item_id), 'Removed field collection item has been deleted.');
  178. // Test removing a field-collection item while creating a new host revision.
  179. list ($node, $item) = $this->createNodeWithFieldCollection();
  180. $node->{$this->field_name}[LANGUAGE_NONE] = array();
  181. $node->revision = TRUE;
  182. node_save($node);
  183. // Item should not be deleted but archived now.
  184. $item = field_collection_item_load($item->item_id);
  185. $this->assertTrue($item, 'Removed field collection item still exists.');
  186. $this->assertTrue($item->archived, 'Removed field collection item is archived.');
  187. // Test removing an old node revision. Make sure that the field collection
  188. // is not removed
  189. list ($node, $item) = $this->createNodeWithFieldCollection();
  190. $node_vid = $node->vid;
  191. $node->revision = TRUE;
  192. node_save($node);
  193. $node_vid2 = $node->vid;
  194. $item_vid2 = $node->{$this->field_name}[LANGUAGE_NONE][0]['revision_id'];
  195. node_revision_delete($node_vid);
  196. $item2 = field_collection_item_revision_load($item_vid2);
  197. $item_id2 = isset($item2->item_id) ? $item2->item_id : -1;
  198. $this->assertEqual($item_id2, $item->item_id, 'Removing an old node revision does not delete newer field collection revisions');
  199. }
  200. /**
  201. * Make sure the basic UI and access checks are working.
  202. */
  203. function testBasicUI() {
  204. // Add a field to the collection.
  205. $field = array(
  206. 'field_name' => 'field_text',
  207. 'type' => 'text',
  208. 'cardinality' => 1,
  209. 'translatable' => FALSE,
  210. );
  211. field_create_field($field);
  212. $instance = array(
  213. 'entity_type' => 'field_collection_item',
  214. 'field_name' => 'field_text',
  215. 'bundle' => $this->field_name,
  216. 'label' => 'Test text field',
  217. 'widget' => array(
  218. 'type' => 'text_textfield',
  219. ),
  220. );
  221. field_create_instance($instance);
  222. $user = $this->drupalCreateUser();
  223. $node = $this->drupalCreateNode(array('type' => 'article'));
  224. $this->drupalLogin($user);
  225. // Make sure access is denied.
  226. $path = 'field-collection/field-test-collection/add/node/' . $node->nid;
  227. $this->drupalGet($path);
  228. $this->assertText(t('Access denied'), 'Access has been denied.');
  229. $user_privileged = $this->drupalCreateUser(array('access content', 'edit any article content'));
  230. $this->drupalLogin($user_privileged);
  231. $this->drupalGet("node/$node->nid");
  232. $this->assertLinkByHref($path, 0, 'Add link is shown.');
  233. $this->drupalGet($path);
  234. $this->assertText(t('Test text field'), 'Add form is shown.');
  235. $edit['field_text[und][0][value]'] = $this->randomName();
  236. $this->drupalPost($path, $edit, t('Save'));
  237. $this->assertText(t('The changes have been saved.'), 'Field collection saved.');
  238. $this->assertText($edit['field_text[und][0][value]'], "Added field value is shown.");
  239. $edit['field_text[und][0][value]'] = $this->randomName();
  240. $this->drupalPost('field-collection/field-test-collection/1/edit', $edit, t('Save'));
  241. $this->assertText(t('The changes have been saved.'), 'Field collection saved.');
  242. $this->assertText($edit['field_text[und][0][value]'], "Field collection has been edited.");
  243. $this->drupalGet('field-collection/field-test-collection/1');
  244. $this->assertText($edit['field_text[und][0][value]'], "Field collection can be viewed.");
  245. // Add further 3 items, so we have reached 4 == maxium cardinality.
  246. $this->drupalPost($path, $edit, t('Save'));
  247. $this->drupalPost($path, $edit, t('Save'));
  248. $this->drupalPost($path, $edit, t('Save'));
  249. // Make sure adding doesn't work any more as we have restricted cardinality
  250. // to 1.
  251. $this->drupalGet($path);
  252. $this->assertText(t('Too many items.'), 'Maxium cardinality has been reached.');
  253. $this->drupalPost('field-collection/field-test-collection/1/delete', array(), t('Delete'));
  254. $this->drupalGet($path);
  255. // Add form is shown again.
  256. $this->assertText(t('Test text field'), 'Field collection item has been deleted.');
  257. // Test the viewing a revision. There should be no links to change it.
  258. $vid = $node->vid;
  259. $node = node_load($node->nid, NULL, TRUE);
  260. $node->revision = TRUE;
  261. node_save($node);
  262. $this->drupalGet("node/$node->nid/revisions/$vid/view");
  263. $this->assertResponse(403, 'Access to view revision denied');
  264. // Login in as admin and try again.
  265. $user = $this->drupalCreateUser(array('administer nodes', 'bypass node access'));
  266. $this->drupalLogin($user);
  267. $this->drupalGet("node/$node->nid/revisions/$vid/view");
  268. $this->assertNoResponse(403, 'Access to view revision granted');
  269. $this->assertNoLinkByHref($path, 'No links on revision view.');
  270. $this->assertNoLinkByHref('field-collection/field-test-collection/2/edit', 'No links on revision view.');
  271. $this->assertNoLinkByHref('field-collection/field-test-collection/2/delete', 'No links on revision view.');
  272. $this->drupalGet("node/$node->nid/revisions");
  273. }
  274. /**
  275. * Make sure that field_collection-entities are copied when host-entities do.
  276. */
  277. public function testCopyingEntities() {
  278. list($node, $entity) = $this->createNodeWithFieldCollection();
  279. // Create a copy of that node.
  280. $node->nid = NULL;
  281. $node->vid = NULL;
  282. $node->is_new = TRUE;
  283. node_save($node);
  284. $item = $node->{$this->field_name}[LANGUAGE_NONE][0];
  285. $this->assertNotEqual($entity->item_id, $item['value']);
  286. // Do a php clone to the $node object and save it.
  287. $node2 = clone $node;
  288. $node2->nid = NULL;
  289. $node2->is_new = TRUE;
  290. $node2->vid = NULL;
  291. node_save($node2);
  292. $item2 = $node2->{$this->field_name}[LANGUAGE_NONE][0];
  293. $this->assertNotEqual($item2['value'], $item['value']);
  294. // Create another copy this time (needlessly) forcing a new revision.
  295. $node->nid = NULL;
  296. $node->vid = NULL;
  297. $node->is_new = TRUE;
  298. $node->revision = TRUE;
  299. node_save($node);
  300. $item3 = $node->{$this->field_name}[LANGUAGE_NONE][0];
  301. $this->assertNotEqual($item['value'], $item3['value']);
  302. }
  303. }
  304. /**
  305. * Test using field collection with Rules.
  306. */
  307. class FieldCollectionRulesIntegrationTestCase extends DrupalWebTestCase {
  308. public static function getInfo() {
  309. return array(
  310. 'name' => 'Field collection Rules integration',
  311. 'description' => 'Tests using field collections with rules.',
  312. 'group' => 'Field types',
  313. 'dependencies' => array('rules'),
  314. );
  315. }
  316. function setUp() {
  317. parent::setUp(array('field_collection', 'rules'));
  318. variable_set('rules_debug_log', 1);
  319. }
  320. protected function createFields($cardinality = 4) {
  321. // Create a field_collection field to use for the tests.
  322. $this->field_name = 'field_test_collection';
  323. $this->field = array('field_name' => $this->field_name, 'type' => 'field_collection', 'cardinality' => $cardinality);
  324. $this->field = field_create_field($this->field);
  325. $this->field_id = $this->field['id'];
  326. $this->instance = array(
  327. 'field_name' => $this->field_name,
  328. 'entity_type' => 'node',
  329. 'bundle' => 'article',
  330. 'label' => $this->randomName() . '_label',
  331. 'description' => $this->randomName() . '_description',
  332. 'weight' => mt_rand(0, 127),
  333. 'settings' => array(),
  334. 'widget' => array(
  335. 'type' => 'hidden',
  336. 'label' => 'Test',
  337. 'settings' => array(),
  338. ),
  339. );
  340. $this->instance = field_create_instance($this->instance);
  341. // Add a field to the collection.
  342. $field = array(
  343. 'field_name' => 'field_text',
  344. 'type' => 'text',
  345. 'cardinality' => 1,
  346. 'translatable' => FALSE,
  347. );
  348. field_create_field($field);
  349. $instance = array(
  350. 'entity_type' => 'field_collection_item',
  351. 'field_name' => 'field_text',
  352. 'bundle' => $this->field_name,
  353. 'label' => 'Test text field',
  354. 'widget' => array(
  355. 'type' => 'text_textfield',
  356. ),
  357. );
  358. field_create_instance($instance);
  359. }
  360. /**
  361. * Test creation field collection items.
  362. */
  363. function testCreation() {
  364. $this->createFields();
  365. $node = $this->drupalCreateNode(array('type' => 'article'));
  366. // Create a field collection.
  367. $action_set = rules_action_set(array('node' => array('type' => 'node', 'bundle' => 'article')));
  368. $action_set->action('entity_create', array(
  369. 'type' => 'field_collection_item',
  370. 'param_field_name' => $this->field_name,
  371. 'param_host_entity:select' => 'node',
  372. ));
  373. $action_set->action('data_set', array('data:select' => 'entity-created:field-text', 'value' => 'foo'));
  374. $action_set->execute($node);
  375. $node = node_load($node->nid, NULL, TRUE);
  376. $this->assertTrue(!empty($node->{$this->field_name}[LANGUAGE_NONE][0]['value']), 'A field_collection has been successfully created.');
  377. $this->assertTrue(!empty($node->{$this->field_name}[LANGUAGE_NONE][0]['revision_id']), 'A field_collection has been successfully created (revision).');
  378. // Now try making use of the field collection in rules.
  379. $action_set = rules_action_set(array('node' => array('type' => 'node', 'bundle' => 'article')));
  380. $action_set->action('drupal_message', array('message:select' => 'node:field-test-collection:0:field-text'));
  381. $action_set->execute($node);
  382. $msg = drupal_get_messages();
  383. $this->assertEqual(array_pop($msg['status']), 'foo', 'Field collection can be used.');
  384. RulesLog::logger()->checkLog();
  385. }
  386. /**
  387. * Test using field collection items via the host while they are being created.
  388. */
  389. function testUsageDuringCreation() {
  390. // Test using a single-cardinality field collection.
  391. $this->createFields(1);
  392. $node = $this->drupalCreateNode(array('type' => 'article'));
  393. $entity = entity_create('field_collection_item', array('field_name' => $this->field_name));
  394. $entity->setHostEntity('node', $node);
  395. // Now the field collection is linked to the host, but not yet saved.
  396. // Test using the wrapper on it.
  397. $wrapper = entity_metadata_wrapper('node', $node);
  398. $wrapper->get($this->field_name)->field_text->set('foo');
  399. $this->assertEqual($entity->field_text[LANGUAGE_NONE][0]['value'], 'foo', 'Field collection item used during creation via the wrapper.');
  400. // Now test it via Rules, which should save our changes.
  401. $set = rules_action_set(array('node' => array('type' => 'node', 'bundle' => 'article')));
  402. $set->action('data_set', array('data:select' => 'node:' . $this->field_name . ':field-text', 'value' => 'bar'));
  403. $set->execute($node);
  404. $this->assertEqual($entity->field_text[LANGUAGE_NONE][0]['value'], 'bar', 'Field collection item used during creation via Rules.');
  405. $this->assertTrue(!empty($entity->item_id) && !empty($entity->revision_id), 'Field collection item has been saved by Rules and the host entity.');
  406. RulesLog::logger()->checkLog();
  407. }
  408. }
  409. /**
  410. * Test using field collection with content that gets translated.
  411. */
  412. class FieldCollectionContentTranslationTestCase extends DrupalWebTestCase {
  413. public static function getInfo() {
  414. return array(
  415. 'name' => 'Field collection content translation',
  416. 'description' => 'Tests using content under translation.',
  417. 'group' => 'Field types',
  418. 'dependencies' => array('translation'),
  419. );
  420. }
  421. public function setUp() {
  422. parent::setUp(array('field_collection', 'translation'));
  423. // Create a field_collection field to use for the tests.
  424. $this->field_name = 'field_test_collection';
  425. $this->field = array('field_name' => $this->field_name, 'type' => 'field_collection', 'cardinality' => 4);
  426. $this->field = field_create_field($this->field);
  427. $this->field_id = $this->field['id'];
  428. $this->instance = array(
  429. 'field_name' => $this->field_name,
  430. 'entity_type' => 'node',
  431. 'bundle' => 'article',
  432. 'label' => $this->randomName() . '_label',
  433. 'description' => $this->randomName() . '_description',
  434. 'weight' => mt_rand(0, 127),
  435. 'settings' => array(),
  436. 'widget' => array(
  437. 'type' => 'field_collection_embed',
  438. 'label' => 'Test',
  439. 'settings' => array(),
  440. ),
  441. );
  442. $this->instance = field_create_instance($this->instance);
  443. // Add a field to the collection.
  444. $field = array(
  445. 'field_name' => 'field_text',
  446. 'type' => 'text',
  447. 'cardinality' => 1,
  448. 'translatable' => FALSE,
  449. );
  450. field_create_field($field);
  451. $instance = array(
  452. 'entity_type' => 'field_collection_item',
  453. 'field_name' => 'field_text',
  454. 'bundle' => $this->field_name,
  455. 'label' => 'Test text field',
  456. 'widget' => array(
  457. 'type' => 'text_textfield',
  458. ),
  459. );
  460. field_create_instance($instance);
  461. $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages', 'create article content', 'edit any article content', 'translate content'));
  462. $this->drupalLogin($admin_user);
  463. // Add German language.
  464. locale_add_language('de');
  465. // Set "Article" content type to use multilingual support.
  466. variable_set('language_content_type_article', TRANSLATION_ENABLED);
  467. }
  468. /**
  469. * Ensure field collections are cloned to new entities on content translation.
  470. */
  471. public function testContentTranslation() {
  472. // Create "Article" content.
  473. $edit['title'] = $this->randomName();
  474. $edit['body[' . LANGUAGE_NONE . '][0][value]'] = $this->randomName();
  475. $edit['language'] = 'en';
  476. $field_collection_name = 'field_test_collection[' . LANGUAGE_NONE . '][0][field_text][' . LANGUAGE_NONE . '][0][value]';
  477. $edit[$field_collection_name] = $this->randomName();
  478. $this->drupalPost('node/add/article', $edit, t('Save'));
  479. $this->assertRaw(t('Article %title has been created.', array('%title' => $edit['title'])), 'Article created.');
  480. $node1 = $this->drupalGetNodeByTitle($edit['title']);
  481. $this->drupalGet('node/' . $node1->nid . '/edit');
  482. $this->drupalGet('node/' . $node1->nid . '/translate');
  483. $this->drupalGet('node/add/article', array('query' => array('translation' => $node1->nid, 'target' => 'de')));
  484. // Suffix translations with the langcode.
  485. unset($edit['language']);
  486. $edit['title'] .= 'DE';
  487. $edit[$field_collection_name] .= 'DE';
  488. $this->drupalPost('node/add/article', $edit, t('Save'), array('query' => array('translation' => $node1->nid, 'target' => 'de')));
  489. $node2 = $this->drupalGetNodeByTitle($edit['title']);
  490. // Ensure that our new node is the translation of the first one.
  491. $this->assertEqual($node1->nid, $node2->tnid, 'Succesfully created translation.');
  492. // And check to see that their field collections are different.
  493. $this->assertNotEqual($node1->field_test_collection, $node2->field_test_collection, 'Field collections between translation source and translation differ.');
  494. }
  495. }