You are here

protected function DeveloperAppFieldTest::fieldStorageFormattersTest in Apigee Edge 8

Tests field storage formatters (CSV and JSON).

1 call to DeveloperAppFieldTest::fieldStorageFormattersTest()
DeveloperAppFieldTest::testFieldableDeveloperApp in tests/src/Functional/DeveloperAppFieldTest.php
Tests fieldable developer app entity.

File

tests/src/Functional/DeveloperAppFieldTest.php, line 132

Class

DeveloperAppFieldTest
Fieldable developer app test.

Namespace

Drupal\Tests\apigee_edge\Functional

Code

protected function fieldStorageFormattersTest() {
  $field_name_prefix = (string) $this
    ->config('field_ui.settings')
    ->get('field_prefix');
  $paragraph = trim($this
    ->getRandomGenerator()
    ->paragraphs(1));
  $paragraphs = trim($this
    ->getRandomGenerator()
    ->paragraphs());
  $link = [
    [
      'uri' => 'http://example.com',
      'title' => 'Example',
      'options' => [],
      'attributes' => [],
    ],
  ];
  $fields = [
    strtolower($this
      ->randomMachineName()) => [
      'type' => 'boolean',
      'data' => [
        [
          'value' => TRUE,
        ],
      ],
      'encoded' => '1',
    ],
    strtolower($this
      ->randomMachineName()) => [
      'type' => 'float',
      'data' => [
        [
          'value' => M_PI,
        ],
        [
          'value' => M_E,
        ],
        [
          'value' => M_EULER,
        ],
      ],
      'encoded' => implode(',', [
        M_PI,
        M_E,
        M_EULER,
      ]),
    ],
    strtolower($this
      ->randomMachineName()) => [
      'type' => 'integer',
      'data' => [
        [
          'value' => 4,
        ],
        [
          'value' => 9,
        ],
      ],
      'encoded' => '4,9',
    ],
    strtolower($this
      ->randomMachineName()) => [
      'type' => 'decimal',
      'data' => [
        [
          'value' => '0.1',
        ],
      ],
      'encoded' => '0.1',
    ],
    strtolower($this
      ->randomMachineName()) => [
      'type' => 'list_float',
      'settings' => [
        'settings[allowed_values]' => implode(PHP_EOL, [
          M_PI,
          M_E,
          M_EULER,
        ]),
      ],
      'data' => [
        [
          'value' => M_PI,
        ],
      ],
      'encoded' => (string) M_PI,
    ],
    strtolower($this
      ->randomMachineName()) => [
      'type' => 'list_integer',
      'settings' => [
        'settings[allowed_values]' => implode(PHP_EOL, [
          1,
          2,
          3,
        ]),
      ],
      'data' => [
        [
          'value' => 2,
        ],
        [
          'value' => 3,
        ],
      ],
      'encoded' => '2,3',
    ],
    strtolower($this
      ->randomMachineName()) => [
      'type' => 'list_string',
      'settings' => [
        'settings[allowed_values]' => implode(PHP_EOL, [
          'qwer',
          'asdf',
          'zxcv',
        ]),
      ],
      'data' => [
        [
          'value' => 'qwer',
        ],
        [
          'value' => 'asdf',
        ],
        [
          'value' => 'zxcv',
        ],
      ],
      'encoded' => 'qwer,asdf,zxcv',
    ],
    strtolower($this
      ->randomMachineName()) => [
      'type' => 'string',
      'data' => [
        [
          'value' => $paragraph,
        ],
      ],
      'encoded' => "\"{$paragraph}\"",
    ],
    strtolower($this
      ->randomMachineName()) => [
      'type' => 'string_long',
      'data' => [
        [
          'value' => $paragraphs,
        ],
      ],
      'encoded' => "\"{$paragraphs}\"",
    ],
    strtolower($this
      ->randomMachineName()) => [
      'type' => 'email',
      'data' => [
        [
          'value' => 'test@example.com',
        ],
        [
          'value' => 'test_2@example.com',
        ],
      ],
      'encoded' => 'test@example.com,test_2@example.com',
    ],
    strtolower($this
      ->randomMachineName()) => [
      'type' => 'timestamp',
      'data' => [
        [
          'value' => 1531212177,
        ],
        [
          'value' => 1531234234,
        ],
      ],
      'encoded' => '1531212177,1531234234',
    ],
    strtolower($this
      ->randomMachineName()) => [
      'type' => 'link',
      'data' => $link,
      'encoded' => json_encode($link),
    ],
  ];

  // Add fields to developer app.
  $add_field_path = Url::fromRoute('apigee_edge.settings.developer_app')
    ->toString();
  foreach ($fields as $name => $data) {
    $this
      ->fieldUIAddNewField($add_field_path, $name, mb_strtoupper($name), $data['type'], ($data['settings'] ?? []) + [
      'cardinality' => -1,
    ], []);
  }
  drupal_flush_all_caches();
  $this->developerApp = DeveloperApp::load($this->developerApp
    ->id());

  // Save field values as developer app entity attributes.
  foreach ($fields as $name => $data) {
    $full_field_name = "{$field_name_prefix}{$name}";
    $this->developerApp
      ->set($full_field_name, $data['data']);
  }
  $this->developerApp
    ->save();

  /** @var \Drupal\apigee_edge\Entity\DeveloperApp $loaded_app */
  $loaded_app = DeveloperApp::load($this->developerApp
    ->id());

  /** @var \Drupal\apigee_edge\SDKConnectorInterface $connector */
  $connector = $this->container
    ->get('apigee_edge.sdk_connector');
  $controller = new DeveloperAppController($connector
    ->getOrganization(), $this->developer
    ->getDeveloperId(), $connector
    ->getClient());

  /** @var \Apigee\Edge\Api\Management\Entity\DeveloperApp $rawLoadedApp */
  $rawLoadedApp = $controller
    ->load($this->developerApp
    ->getName());
  foreach ($fields as $name => $data) {
    $full_field_name = "{$field_name_prefix}{$name}";
    $this
      ->assertEquals($data['data'], $loaded_app
      ->get($full_field_name)
      ->getValue());
    $this
      ->assertEquals($data['encoded'], $rawLoadedApp
      ->getAttributeValue($name));
  }
}