function TMGMTEntityTestCaseUtility::createTaxonomyTerm in Translation Management Tool 7
Creates a taxonomy term of a given vocabulary.
It uses $this->field_names to populate content of attached fields. You can access fields values using $this->field_names['taxonomy_term'][$vocabulary->machine_name].
Parameters
object $vocabulary: Vocabulary object for which the term should be created.
string $langcode: The language code to be set as the entity source language.
Return value
object Newly created node object.
3 calls to TMGMTEntityTestCaseUtility::createTaxonomyTerm()
- TMGMTEntitySourceLanguageNoneTestCase::testLanguageNeutral in sources/
entity/ tmgmt_entity.source.none.test - Test if language neutral entities are not allowed for translation.
- TMGMTEntitySourceTestCase::testAddingJobItemsWithEmptySourceText in sources/
entity/ tmgmt_entity.source.test - TMGMTEntitySourceTestCase::testEntitySourceTerm in sources/
entity/ tmgmt_entity.source.test - Tests taxonomy terms field translation.
File
- tests/
tmgmt.base.entity.test, line 215
Class
- TMGMTEntityTestCaseUtility
- Utility test case class with helper methods to create entities and their fields with populated translatable content. Extend this class if you create tests in which you need Drupal entities and/or fields.
Code
function createTaxonomyTerm($vocabulary, $langcode = 'en') {
// When an entity is being saved, the entity_translation module initializes
// a translation fetching the language from an entity. But the taxonomy
// terms have no entity language key, so its langcode will be the set to the
// default one.
/* @see entity_translation_field_attach_insert() */
/* @see EntityTranslationDefaultHandler::initTranslations() */
/* @see EntityTranslationDefaultHandler::getLanguage() */
$settings_variable_name = 'entity_translation_settings_taxonomy_term__' . $vocabulary->machine_name;
variable_set($settings_variable_name, array(
'default_language' => $langcode,
));
$term = new stdClass();
$term->name = $this
->randomName();
$term->description = $this
->randomName();
$term->vid = $vocabulary->vid;
foreach ($this->field_names['taxonomy_term'][$vocabulary->machine_name] as $field_name) {
$field_info = field_info_field($field_name);
$cardinality = $field_info['cardinality'] == FIELD_CARDINALITY_UNLIMITED ? 1 : $field_info['cardinality'];
$field_lang = $field_info['translatable'] ? $langcode : LANGUAGE_NONE;
// Create two deltas for each field.
for ($delta = 0; $delta <= $cardinality; $delta++) {
$term->{$field_name}[$field_lang][$delta]['value'] = $this
->randomName(20);
if ($field_info['type'] == 'text_with_summary') {
$term->{$field_name}[$field_lang][$delta]['summary'] = $this
->randomName(10);
}
}
}
taxonomy_term_save($term);
return taxonomy_term_load($term->tid);
}