Problem/Motivation

\Drupal\path\Plugin\Field\FieldType\PathItem extends FieldItemBase which defines its main property name as simply 'value'. This is incorrect for PathItem which should instead be alias.

Proposed resolution

Override mainPropertyName() method on PathItem

Remaining tasks

Tests?

User interface changes

None

API changes

None

Data model changes

None

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

EclipseGc created an issue. See original summary.

EclipseGc’s picture

Patch!

Eclipse

EclipseGc’s picture

Status: Active » Needs review
klausi’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Makes total sense, we should add a tiny unit test that asserts the output of this method.

EclipseGc’s picture

Should there not be a larger unit test that asserts that the main property value is one of the defined properties of the field? In this case right now, 'value' isn't even an option and that feels like it should throw a big fat error somewhere instead of blowing up when I ask for ->getDataType() on a returned NULL value. How I found this is thus:


$property_definition->getFieldStorageDefinition()->getPropertyDefinition($property_definition->getFieldStorageDefinition()->getMainPropertyName())->getDataType();

$property_definition->getFieldStorageDefinition()->getMainPropertyName() returns 'value' and $property_definition->getFieldStorageDefinition()->getPropertyDefinition('value') returns NULL, so NULL->getDataType() doesn't work, and this could magically start failing again for FieldType that makes this same mistake. While ideally I think I'd prefer an optional exception thrown for the getPropertyDefinition() method, I realize that's an api change, and for the moment I think I'd just like to see every field type tested in a loop proving that their main property actually exists on the field somewhere. Is that reasonable?

Eclipse

klausi’s picture

Oh yes, I'd like to see such a test. How would you make sure you catch all possible fields provided by all possible modules in core?

EclipseGc’s picture

Status: Needs work » Needs review
FileSize
1.39 KB

Ok, first pass at the test, ironically, path doesn't seem to show up there... probably because path module's not on. But even this test right now shows we have problems.

Eclipse

Status: Needs review » Needs work

The last submitted patch, 7: 2683431-7-test.patch, failed testing.

dsnopek’s picture

Title: PathItem does not document its main property » PathItem does return correct main property from mainPropertyName()

Re-titling in an attempt to understand the issue. This isn't about documentation, but returning the name of it's main property via the API.

dsnopek’s picture

Title: PathItem does return correct main property from mainPropertyName() » PathItem doesn't return correct main property from mainPropertyName()

Er, it doesN'T do that :-)

penyaskito’s picture

Status: Needs work » Needs review
FileSize
1.42 KB

Rerolled the tests at #7.

Status: Needs review » Needs work

The last submitted patch, 12: 2683431-12-test.patch, failed testing.

penyaskito’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
2.68 KB
1.79 KB
2.92 KB

#7: "path doesn't seem to show up there... probably because path module's not on. But even this test right now shows we have problems."

Adding path to the enabled modules ensures those errors are discovered.

Edited: Also modified the tests so we cover cases with no properties defined, as the MapItem does.

Also fixed ShapeItem while we are here, so the tests are green after the patch. I'm wondering how could we ensure that at least all core modules are added, so we check all the field type plugins.

dawehner’s picture

  1. +++ b/core/modules/field/tests/src/Kernel/FieldTypePluginManagerTest.php
    @@ -13,6 +13,14 @@
    +  /**
    +   * Modules to enable.
    +   *
    +   * @var array
    +   */
    

    Let's use {@inheritdoc}

  2. +++ b/core/modules/field/tests/src/Kernel/FieldTypePluginManagerTest.php
    @@ -13,6 +13,14 @@
    +  public static $modules = ['user', 'system', 'field', 'text', 'entity_test', 'field_test', 'path'];
    

    what about file and image, just to be sure. $this->enableModules

  3. +++ b/core/modules/field/tests/src/Kernel/FieldTypePluginManagerTest.php
    @@ -84,4 +92,19 @@ public function testCreateInstanceWithConfig() {
    +  public function testMainProperty() {
    

    Super cool idea!

  4. +++ b/core/modules/field/tests/src/Kernel/FieldTypePluginManagerTest.php
    @@ -84,4 +92,19 @@ public function testCreateInstanceWithConfig() {
    +        $message = SafeMarkup::format("@plugin property @property found in @properties", ['@plugin' => $plugin_id, '@property' => $property, '@properties' => $properties]);
    

    we use sprintf / literal php strings now.

The last submitted patch, 14: 2683431-14.only-test.patch, failed testing.

penyaskito’s picture

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Cool, thank you!

penyaskito’s picture

FQCN (see interdiff)

The last submitted patch, 17: 2683431-17.only-tests.patch, failed testing.

penyaskito’s picture

Issue tags: +neworleans2016
penyaskito’s picture

Closed #2622300: PathItem must override mainPropertyName() as duplicated, it's older but tests are much better here.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/field/tests/src/Kernel/FieldTypePluginManagerTest.php
@@ -84,4 +86,29 @@ public function testCreateInstanceWithConfig() {
+    $module_list = array_filter(array_keys($module_list), function($module) use ($module_handler) {
+      return !$module_handler->moduleExists($module);
+    });

This should be limited to core modules only. Test modules should be chosen to be enabled. I was wondering if this includes test modules too - imho test modules shouldn't be enabled here either.

alexpott’s picture

  1. +++ b/core/modules/field/tests/src/Kernel/FieldTypePluginManagerTest.php
    @@ -3,6 +3,8 @@
    +use Drupal\Core\Extension\ModuleHandlerInterface;
    

    Not used.

  2. +++ b/core/modules/field/tests/src/Kernel/FieldTypePluginManagerTest.php
    @@ -84,4 +86,29 @@ public function testCreateInstanceWithConfig() {
     
    +  public function testMainProperty() {
    

    Needs a docblock.

  3. +++ b/core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/ShapeItem.php
    @@ -77,4 +77,12 @@ public function isEmpty() {
    +    return 'color';
    

    Not shape?

penyaskito’s picture

Status: Needs work » Needs review
FileSize
2.34 KB
3.45 KB

#23: Tests module were being installed. Filtering now that only core modules and non-testing modules are installed. The only way I knew for filtering those is looking at their path. Please suggest a better way if any.

#24: Fixed.

Status: Needs review » Needs work

The last submitted patch, 25: 2683431-25.patch, failed testing.

penyaskito’s picture

Re-testing, that doesn't look related.

penyaskito’s picture

Status: Needs work » Needs review

Back to needs review then :-)

dawehner’s picture

  1. +++ b/core/modules/field/tests/src/Kernel/FieldTypePluginManagerTest.php
    @@ -84,4 +85,33 @@ public function testCreateInstanceWithConfig() {
    +    $listing = new ExtensionDiscovery(\Drupal::root());
    +    $module_list = $listing->scan('module', FALSE);
    +    /** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */
    +    $module_handler = $this->container->get('module_handler');
    +    $module_list = array_filter(array_keys($module_list), function($module) use ($module_handler, $module_list) {
    +      return !$module_handler->moduleExists($module) && substr($module_list[$module]->getPath(), 0, 4) === 'core';
    +    });
    +    $this->enableModules($module_list);
    

    Let's move that into its own method

  2. +++ b/core/modules/field/tests/src/Kernel/FieldTypePluginManagerTest.php
    @@ -84,4 +85,33 @@ public function testCreateInstanceWithConfig() {
    +        $this->assertTrue(in_array($property, array_keys($class::propertyDefinitions($storage_definition))), $message);
    

    We can use $this->assertArrayKeyExists or so

penyaskito’s picture

Here you are.

Status: Needs review » Needs work

The last submitted patch, 30: 2683431-30.patch, failed testing.

penyaskito’s picture

Status: Needs work » Needs review
FileSize
837 bytes
3.57 KB

Ooopsie

Status: Needs review » Needs work

The last submitted patch, 32: 2683431-32.patch, failed testing.

penyaskito’s picture

Status: Needs work » Needs review
dawehner’s picture

Status: Needs review » Reviewed & tested by the community

It was @penyaskito!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 8f61056 and pushed to 8.1.x. Thanks!

  • alexpott committed 5da0092 on 8.2.x
    Issue #2683431 by penyaskito, EclipseGc, dawehner: PathItem doesn't...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.