You are here

public static function MockData::createItem in GatherContent 8.4

Same name and namespace in other branches
  1. 8.5 tests/modules/gathercontent_test/src/MockData.php \Drupal\gathercontent_test\MockData::createItem()

Creates a GC Item corresponding to a mapping.

2 calls to MockData::createItem()
ContentProcessorTest::testCreateNode in tests/src/Kernel/ContentProcessorTest.php
Data provider for createNodeTest.
ImporterTest::testImport in tests/src/Kernel/ImporterTest.php
Test the import function.

File

tests/modules/gathercontent_test/src/MockData.php, line 85

Class

MockData
A class for getting static test data.

Namespace

Drupal\gathercontent_test

Code

public static function createItem(Mapping $mapping, array $selectedCheckboxes, array $selectedRadioboxes) {
  $mappingData = unserialize($mapping
    ->getData());
  $mainTabElements = reset($mappingData)['elements'];
  $mainTabId = key($mappingData);
  $template = unserialize($mapping
    ->getTemplate())->data;
  $tabs = $template->config;
  $item = new Item();
  $item->id = static::getUniqueInt();
  $item->name = 'test item name ' . $item->id;
  $item->projectId = $template->project_id;
  $item->templateId = $template->id;
  foreach ($tabs as $i => $tab) {
    $newTab = new Tab(json_decode(json_encode($tab), TRUE));
    foreach ($newTab->elements as $element) {
      switch ($element->type) {
        case 'text':
          if ($element->plainText) {
            if ($newTab->id === MockData::METATAG_TAB) {

              // Metatag.
              $element
                ->setValue($element->label . ' ' . static::getUniqueInt());
            }
            else {

              // Title.
              $element
                ->setValue($item->name . ($newTab->id === MockData::TRANSLATED_TAB ? ' translated' : ''));
            }
          }
          else {

            // If translation.
            if ($newTab->id === MockData::TRANSLATED_TAB) {

              // Get the original element value and append 'translated' to it.
              $fieldId = $mappingData[$newTab->id]['elements'][$element->id];
              $mainElementId = array_search($fieldId, $mainTabElements);
              $element
                ->setValue($item->config[$mainTabId]->elements[$mainElementId]
                ->getValue() . ' translated');
            }
            else {
              $element
                ->setValue('test text ' . static::getUniqueInt());
            }
          }
          break;
        case 'files':

          // Files are not stored here, only file field definitions.
          break;
        case 'section':

          // If translation.
          if ($newTab->id === MockData::TRANSLATED_TAB) {
            $fieldId = $mappingData[$newTab->id]['elements'][$element->id];
            $mainElementId = array_search($fieldId, $mainTabElements);
            $element->subtitle = $item->config[$mainTabId]->elements[$mainElementId]->subtitle . ' translated';
          }
          else {
            $element->subtitle = 'test section subtitle ' . static::getUniqueInt();
          }
          break;
        case 'choice_checkbox':
          foreach ($element->options as $i => $option) {
            $element->options[$i]['selected'] = $selectedCheckboxes[$i];
          }
          break;
        case 'choice_radio':
          foreach ($element->options as $i => $option) {
            $element->options[$i]['selected'] = $selectedRadioboxes[$i];
          }
          break;
      }
    }
    $item->config[$newTab->id] = $newTab;
  }
  return $item;
}