tmgmt.base.entity.test 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /*
  3. * @file
  4. * Contains tests for Translation management
  5. */
  6. /**
  7. * Utility test case class with helper methods to create entities and their
  8. * fields with populated translatable content. Extend this class if you create
  9. * tests in which you need Drupal entities and/or fields.
  10. */
  11. abstract class TMGMTEntityTestCaseUtility extends TMGMTBaseTestCase {
  12. public $field_names = array();
  13. /**
  14. * Creates node type with several text fields with different cardinality.
  15. *
  16. * Internally it calls TMGMTEntityTestCaseUtility::attachFields() to create
  17. * and attach fields to newly created bundle. You can than use
  18. * $this->field_names['node']['YOUR_BUNDLE_NAME'] to access them.
  19. *
  20. * @param string $machine_name
  21. * Machine name of the node type.
  22. * @param string $human_name
  23. * Human readable name of the node type.
  24. * @param int $language_content_type
  25. * Either 0 (disabled), 1 (language enabled but no translations),
  26. * TRANSLATION_ENABLED or ENTITY_TRANSLATION_ENABLED.
  27. * pparam bool $attach_fields
  28. * (optional) If fields with the same translatability should automatically
  29. * be attached to the node type.
  30. */
  31. function createNodeType($machine_name, $human_name, $language_content_type = 0, $attach_fields = TRUE) {
  32. // Create new bundle.
  33. $type = array(
  34. 'type' => $machine_name,
  35. 'name' => $human_name,
  36. 'base' => 'node_content',
  37. 'description' => '',
  38. 'custom' => 1,
  39. 'modified' => 1,
  40. 'locked' => 0,
  41. );
  42. $type = node_type_set_defaults($type);
  43. node_type_save($type);
  44. node_add_body_field($type);
  45. node_types_rebuild();
  46. // Set content type to be translatable as specified by
  47. // $language_content_type.
  48. $edit = array();
  49. $edit['language_content_type'] = $language_content_type;
  50. $this->drupalPost('admin/structure/types/manage/' . $machine_name, $edit, t('Save content type'));
  51. $translatable = FALSE;
  52. if (defined('ENTITY_TRANSLATION_ENABLED') && $language_content_type == ENTITY_TRANSLATION_ENABLED) {
  53. $translatable = TRUE;
  54. }
  55. // Push in also the body field.
  56. $this->field_names['node'][$machine_name][] = 'body';
  57. if ($attach_fields) {
  58. $this->attachFields('node', $machine_name, $translatable);
  59. }
  60. // Change body field to be translatable.
  61. $body = field_info_field('body');
  62. $body['translatable'] = $translatable;
  63. field_update_field($body);
  64. }
  65. /**
  66. * Creates taxonomy vocabulary with custom fields.
  67. *
  68. * To create and attach fields it internally calls
  69. * TMGMTEntityTestCaseUtility::attachFields(). You can than access these
  70. * fields calling $this->field_names['node']['YOUR_BUNDLE_NAME'].
  71. *
  72. * @param string $machine_name
  73. * Vocabulary machine name.
  74. * @param string $human_name
  75. * Vocabulary human readable name.
  76. * @param bool|array $fields_translatable
  77. * Flag or definition array to determine which or all fields should be
  78. * translatable.
  79. *
  80. * @return stdClass
  81. * Created vocabulary object.
  82. */
  83. function createTaxonomyVocab($machine_name, $human_name, $fields_translatable = TRUE) {
  84. $vocabulary = new stdClass();
  85. $vocabulary->name = $human_name;
  86. $vocabulary->machine_name = $machine_name;
  87. taxonomy_vocabulary_save($vocabulary);
  88. $this->attachFields('taxonomy_term', $vocabulary->machine_name, $fields_translatable);
  89. return $vocabulary;
  90. }
  91. /**
  92. * Creates fields of type text and text_with_summary of different cardinality.
  93. *
  94. * It will attach created fields to provided entity name and bundle.
  95. *
  96. * Field names will be stored in $this->field_names['entity']['bundle']
  97. * through which you can access them.
  98. *
  99. * @param string $entity_name
  100. * Entity name to which fields should be attached.
  101. * @param string $bundle
  102. * Bundle name to which fields should be attached.
  103. * @param bool|array $translatable
  104. * Flag or definition array to determine which or all fields should be
  105. * translatable.
  106. */
  107. function attachFields($entity_name, $bundle, $translatable = TRUE) {
  108. // Create several text fields.
  109. $field_types = array('text', 'text_with_summary');
  110. for ($i = 0 ; $i <= 5; $i++) {
  111. $field_type = $field_types[array_rand($field_types, 1)];
  112. $field_name = drupal_strtolower($this->randomName());
  113. // Create a field.
  114. $field = array(
  115. 'field_name' => $field_name,
  116. 'type' => $field_type,
  117. 'cardinality' => mt_rand(1, 5),
  118. 'translatable' => is_array($translatable) && isset($translatable[$i]) ? $translatable[$i] : (boolean) $translatable,
  119. );
  120. field_create_field($field);
  121. // Create an instance of the previously created field.
  122. $instance = array(
  123. 'field_name' => $field_name,
  124. 'entity_type' => $entity_name,
  125. 'bundle' => $bundle,
  126. 'label' => $this->randomName(10),
  127. 'description' => $this->randomString(30),
  128. 'widget' => array(
  129. 'type' => $field_type == 'text' ? 'text_textfield' : 'text_textarea_with_summary',
  130. 'label' => $this->randomString(10),
  131. ),
  132. );
  133. field_create_instance($instance);
  134. // Store field names in case there are needed outside this method.
  135. $this->field_names[$entity_name][$bundle][] = $field_name;
  136. }
  137. }
  138. /**
  139. * Creates a node of a given bundle.
  140. *
  141. * It uses $this->field_names to populate content of attached fields.
  142. *
  143. * @param string $bundle
  144. * Node type name.
  145. * @param string $sourcelang
  146. * Source lang of the node to be created.
  147. *
  148. * @return object
  149. * Newly created node object.
  150. */
  151. function createNode($bundle, $sourcelang = 'en') {
  152. $node = array(
  153. 'type' => $bundle,
  154. 'language' => $sourcelang,
  155. // Ensure that the body field is defined for the node language.
  156. 'body' => array($sourcelang => array(0 => array())),
  157. );
  158. foreach ($this->field_names['node'][$bundle] as $field_name) {
  159. $field_info = field_info_field($field_name);
  160. $cardinality = $field_info['cardinality'] == FIELD_CARDINALITY_UNLIMITED ? 1 : $field_info['cardinality'];
  161. $field_langcode = field_is_translatable('node', $field_info) ? $sourcelang : LANGUAGE_NONE;
  162. // Create two deltas for each field.
  163. for ($delta = 0; $delta <= $cardinality; $delta++) {
  164. $node[$field_name][$field_langcode][$delta]['value'] = $this->randomName(20);
  165. if ($field_info['type'] == 'text_with_summary') {
  166. $node[$field_name][$field_langcode][$delta]['summary'] = $this->randomName(10);
  167. }
  168. }
  169. }
  170. return $this->drupalCreateNode($node);
  171. }
  172. /**
  173. * Creates a taxonomy term of a given vocabulary.
  174. *
  175. * It uses $this->field_names to populate content of attached fields. You can
  176. * access fields values using
  177. * $this->field_names['taxonomy_term'][$vocabulary->machine_name].
  178. *
  179. * @param object $vocabulary
  180. * Vocabulary object for which the term should be created.
  181. *
  182. * @param string $langcode
  183. * The language code to be set as the entity source language.
  184. *
  185. * @return object
  186. * Newly created node object.
  187. */
  188. function createTaxonomyTerm($vocabulary, $langcode = 'en') {
  189. // When an entity is being saved, the entity_translation module initializes
  190. // a translation fetching the language from an entity. But the taxonomy
  191. // terms have no entity language key, so its langcode will be the set to the
  192. // default one.
  193. /* @see entity_translation_field_attach_insert() */
  194. /* @see EntityTranslationDefaultHandler::initTranslations() */
  195. /* @see EntityTranslationDefaultHandler::getLanguage() */
  196. $settings_variable_name = 'entity_translation_settings_taxonomy_term__' . $vocabulary->machine_name;
  197. variable_set($settings_variable_name, array('default_language' => $langcode));
  198. $term = new stdClass();
  199. $term->name = $this->randomName();
  200. $term->description = $this->randomName();
  201. $term->vid = $vocabulary->vid;
  202. foreach ($this->field_names['taxonomy_term'][$vocabulary->machine_name] as $field_name) {
  203. $field_info = field_info_field($field_name);
  204. $cardinality = $field_info['cardinality'] == FIELD_CARDINALITY_UNLIMITED ? 1 : $field_info['cardinality'];
  205. $field_lang = $field_info['translatable'] ? $langcode : LANGUAGE_NONE;
  206. // Create two deltas for each field.
  207. for ($delta = 0; $delta <= $cardinality; $delta++) {
  208. $term->{$field_name}[$field_lang][$delta]['value'] = $this->randomName(20);
  209. if ($field_info['type'] == 'text_with_summary') {
  210. $term->{$field_name}[$field_lang][$delta]['summary'] = $this->randomName(10);
  211. }
  212. }
  213. }
  214. taxonomy_term_save($term);
  215. return taxonomy_term_load($term->tid);
  216. }
  217. }