Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding patch for field core module #1058

Open
wants to merge 14 commits into
base: 3.x
Choose a base branch
from
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ jobs:
composer require wikimedia/composer-merge-plugin
composer config --json extra.merge-plugin.require '["modules/contrib/apigee_edge/composer.json"]'
composer config platform.php ${{ matrix.php-version }}
composer config --json extra.patches."drupal/core" '{ "Support entities that are neither content nor config entities": "https://www.drupal.org/files/issues/2020-12-02/3042467-50.patch", "Add a method to access the original property": "https://www.drupal.org/files/issues/2023-07-22/2839195-105.patch"}'
composer config --json extra.patches."drupal/core" '{ "TypeError: FieldTypePluginManager::createFieldItem() called in FieldStorageConfig.php": "https://www.drupal.org/files/issues/2024-05-27/field-typeerror-3450175-3.patch", "Support entities that are neither content nor config entities": "https://www.drupal.org/files/issues/2020-12-02/3042467-50.patch", "Add a method to access the original property": "https://www.drupal.org/files/issues/2023-07-22/2839195-105.patch"}'
composer update --with-all-dependencies
composer require --dev phpspec/prophecy-phpunit:^2
composer require --dev drupal/classy:^1.0
Expand Down
180 changes: 139 additions & 41 deletions tests/src/Functional/DeveloperAppFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -70,6 +72,27 @@ class DeveloperAppFieldTest extends ApigeeEdgeFunctionalTestBase {
*/
protected $developerApp;

/**
* Array of list fields.
*
* @var array
*/
protected $listFields = [];

/**
* Name of the option field.
*
* @var string
*/
protected $fieldName;

/**
* Admin path to manage field storage settings.
*
* @var string
*/
protected $adminPath;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -179,47 +202,6 @@ protected function fieldStorageFormattersTest() {
],
'encoded' => '0.1',
],
strtolower($this->randomMachineName()) => [
'type' => 'list_float',
'settings' => [
'settings[allowed_values]' => implode(PHP_EOL, [
round(M_PI, 10),
round(M_E, 10),
round(M_EULER, 10),
]),
],
'data' => [
['value' => round(M_PI, 10)],
],
'encoded' => (string) round(M_PI, 10),
],
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' => [
Expand Down Expand Up @@ -257,6 +239,62 @@ protected function fieldStorageFormattersTest() {
],
];

$this->listFields = [
'list_integer' => [
'name' => strtolower($this->randomMachineName()),
'settings' => [
'field_storage[subform][settings][allowed_values][table][0][item][key]' => 0,
'field_storage[subform][settings][allowed_values][table][0][item][label]' => 'Zero',
'field_storage[subform][settings][allowed_values][table][1][item][key]' => 1,
'field_storage[subform][settings][allowed_values][table][1][item][label]' => 'One',
],
'data' => [
0 => 'Zero',
1 => 'One'
],
'encoded' => '0,1',
],
'list_string' => [
'name' => strtolower($this->randomMachineName()),
'settings' => [
'field_storage[subform][settings][allowed_values][table][0][item][key]' => 'zero',
'field_storage[subform][settings][allowed_values][table][0][item][label]' => 'Zero',
'field_storage[subform][settings][allowed_values][table][1][item][key]' => 'one',
'field_storage[subform][settings][allowed_values][table][1][item][label]' => 'One',
],
'data' => [
'zero' => 'Zero',
'one' => 'One'
],
'encoded' => 'zero,one',
],
'list_float' => [
'name' => strtolower($this->randomMachineName()),
'settings' => [
'field_storage[subform][settings][allowed_values][table][0][item][key]' => 0,
'field_storage[subform][settings][allowed_values][table][0][item][label]' => 'Zero',
'field_storage[subform][settings][allowed_values][table][1][item][key]' => .5,
'field_storage[subform][settings][allowed_values][table][1][item][label]' => 'Point five',
'field_storage[subform][settings][allowed_values][table][2][item][key]' => 2,
'field_storage[subform][settings][allowed_values][table][2][item][label]' => 'Two',
],
'data' => [
'0' => 'Zero',
'0.5' => 'Point five',
'2' => 'Two'
],
'encoded' => '0.5',
]
];

// Changes for field of types 'list' fields
// Using field configs to save the fields as issue is faced by FieldUiTestTrait.
foreach ($this->listFields as $list_type => $listData) {
$this->fieldName = 'field_' . $listData['name'];
$this->createOptionsField($list_type);
$this->assertAllowedValuesInput($listData['settings'], $listData['data'], '');
}

// Add fields to developer app.
$add_field_path = Url::fromRoute('apigee_edge.settings.developer_app')->toString();
foreach ($fields as $name => $data) {
Expand Down Expand Up @@ -587,4 +625,64 @@ protected function assertFieldVisibleOnEntityDisplay(string $app_name, string $f
}
}

/**
* Helper function to create list field of a given type.
*
* @param string $type
* One of 'list_integer', 'list_float' or 'list_string'.
*/
protected function createOptionsField($type) {
// Create a field.
FieldStorageConfig::create([
'field_name' => $this->fieldName,
'entity_type' => 'developer_app',
'type' => $type,
])->save();
FieldConfig::create([
'field_name' => $this->fieldName,
'entity_type' => 'developer_app',
'bundle' => 'developer_app',
])->save();

\Drupal::service('entity_display.repository')
->getFormDisplay('developer_app', 'developer_app')
->setComponent($this->fieldName)
->save();

$this->adminPath = 'admin/config/apigee-edge/app-settings/developer-apps/fields/developer_app.developer_app.' . $this->fieldName;
}

/**
* Tests an input array for the 'allowed values' form element.
*
* @param array $input
* The input array.
* @param array|string $result
* Either an expected resulting array in
* $field->getSetting('allowed_values'), or an expected error message.
* @param string $message
* Message to display.
*
* @internal
*/
public function assertAllowedValuesInput(array $input, $result, string $message): void {
$this->drupalGet($this->adminPath);
$page = $this->getSession()->getPage();
$add_button = $page->findButton('Add another item');
$add_button->click();
$add_button->click();

$this->submitForm($input, 'Save');
// Verify that the page does not have double escaped HTML tags.
$this->assertSession()->responseNotContains('<');

if (is_string($result)) {
$this->assertSession()->pageTextContains($result);
}
else {
$field_storage = FieldStorageConfig::loadByName('developer_app', $this->fieldName);
$this->assertSame($field_storage->getSetting('allowed_values'), $result, $message);
}
}

}
Loading
Loading