You are here

protected function DeveloperSyncTest::setUpUserFields in Apigee Edge 8

Creates fields for Drupal users.

1 call to DeveloperSyncTest::setUpUserFields()
DeveloperSyncTest::setUp in tests/src/Functional/DeveloperSyncTest.php

File

tests/src/Functional/DeveloperSyncTest.php, line 266

Class

DeveloperSyncTest
Developer-user synchronization test.

Namespace

Drupal\Tests\apigee_edge\Functional

Code

protected function setUpUserFields() {
  $text = $this
    ->getRandomGenerator()
    ->sentences(5);
  $link = [
    [
      'title' => 'Example',
      'options' => [],
      'uri' => 'http://example.com',
    ],
  ];
  $link_changed = [
    [
      'title' => 'Example_Changed',
      'options' => [],
      'uri' => 'http://example.com/changed',
    ],
  ];
  $this->fields = [
    'boolean' => [
      'name' => strtolower($this
        ->randomMachineName()),
      'data' => [
        [
          'value' => 1,
        ],
      ],
      'data_changed' => [
        [
          'value' => 0,
        ],
      ],
    ],
    'email' => [
      'name' => strtolower($this
        ->randomMachineName()),
      'data' => [
        [
          'value' => 'test@example.com',
        ],
      ],
      'data_changed' => [
        [
          'value' => 'test.changed@example.com',
        ],
      ],
    ],
    'timestamp' => [
      'name' => strtolower($this
        ->randomMachineName()),
      'data' => [
        [
          'value' => 1531212177,
        ],
      ],
      'data_changed' => [
        [
          'value' => 1531000000,
        ],
      ],
    ],
    'integer' => [
      'name' => strtolower($this
        ->randomMachineName()),
      'data' => [
        [
          'value' => 4,
        ],
        [
          'value' => 9,
        ],
      ],
      'data_changed' => [
        [
          'value' => 2,
        ],
        [
          'value' => 8,
        ],
        [
          'value' => 1,
        ],
      ],
    ],
    'list_integer' => [
      'name' => strtolower($this
        ->randomMachineName()),
      'settings' => [
        'settings[allowed_values]' => implode(PHP_EOL, [
          1,
          2,
          3,
        ]),
      ],
      'data' => [
        [
          'value' => 2,
        ],
        [
          'value' => 3,
        ],
      ],
      'data_changed' => [
        [
          'value' => 1,
        ],
        [
          'value' => 3,
        ],
      ],
    ],
    'list_string' => [
      'name' => strtolower($this
        ->randomMachineName()),
      'settings' => [
        'settings[allowed_values]' => implode(PHP_EOL, [
          'qwer',
          'asdf',
          'zxcv',
        ]),
      ],
      'data' => [
        [
          'value' => 'qwer',
        ],
        [
          'value' => 'asdf',
        ],
        [
          'value' => 'zxcv',
        ],
      ],
      'data_changed' => [
        [
          'value' => 'qwer',
        ],
        [
          'value' => 'asdf',
        ],
      ],
    ],
    'string' => [
      'name' => strtolower($this
        ->randomMachineName()),
      'data' => [
        [
          'value' => $text,
        ],
      ],
      'data_changed' => [
        [
          'value' => strrev($text),
        ],
      ],
    ],
    'string_long' => [
      'name' => strtolower($this
        ->randomMachineName()),
      'data' => [
        [
          'value' => $text,
        ],
      ],
      'data_changed' => [
        [
          'value' => strrev($text),
        ],
      ],
    ],
    'link' => [
      'name' => strtolower($this
        ->randomMachineName()),
      'data' => $link,
      'data_changed' => $link_changed,
    ],
  ];
  foreach ($this->fields as $field_type => $data) {
    $this
      ->fieldUIAddNewField(Url::fromRoute('entity.user.admin_form')
      ->toString(), $data['name'], mb_strtoupper($data['name']), $field_type, ($data['settings'] ?? []) + [
      'cardinality' => -1,
    ], []);
  }

  // Create a Drupal user field that is not linked to any Apigee Edge
  // developer attribute. It should be unchanged after sync on both sides.
  $this
    ->fieldUIAddNewField(Url::fromRoute('entity.user.admin_form')
    ->toString(), 'one_track_field', strtoupper('one_track_field'), 'string', [
    'cardinality' => -1,
  ], []);

  // Create a Drupal user email field that has an invalid value on Apigee Edge
  // (invalid email address). The invalid value should not be copied into the
  // Drupal user's field.
  $this
    ->fieldUIAddNewField(Url::fromRoute('entity.user.admin_form')
    ->toString(), 'invalid_email', strtoupper('invalid_email'), 'email', [
    'cardinality' => -1,
  ], []);
  drupal_flush_all_caches();

  // Set the fields to be synchronized.
  $this
    ->drupalGet(Url::fromRoute('apigee_edge.settings.developer.attributes'));
  $full_field_names = [];
  foreach ($this->fields as $field_type => $data) {
    $full_field_name = "{$this->fieldNamePrefix}{$data['name']}";
    $this
      ->getSession()
      ->getPage()
      ->checkField("attributes[{$full_field_name}]");
    $full_field_names[] = $full_field_name;
  }
  $this
    ->getSession()
    ->getPage()
    ->checkField("attributes[{$this->fieldNamePrefix}invalid_email]");
  $full_field_names[] = "{$this->fieldNamePrefix}invalid_email";
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Save configuration');
  $this
    ->assertSession()
    ->pageTextContains('The configuration options have been saved.');
  $user_fields_to_sync = $this
    ->config('apigee_edge.sync')
    ->get('user_fields_to_sync');
  $this
    ->assertEquals(asort($user_fields_to_sync), asort($full_field_names));
}