Problem/Motivation

Exporting configurations is now supported and helps with performance when creating an array from the schema file. Since a recent change, ConfigEntityType annotation can declare an array of config entities to export, which saves a lot of time, for example during site install.

Proposed resolution

Apply the patch. Look at validator errors and fix the schema file.

Remaining tasks

None

User interface changes

None

API changes

None

Comments

LKS90’s picture

Status: Active » Needs review
StatusFileSize
new1.14 KB
xano’s picture

Thanks! Does this solve a particular problem? Both entity classes already override ::toArray() to build the export array. Currencies definitely need additional logic to export usages. The annotation will not be sufficient to achieve that.

LKS90’s picture

Hey there!

To be honest, I'm not really sure. Some colleagues complained about slow site installs and exporting the config supposedly speeds that up. So they gave me the task to add the config_export array to all contribution modules. Berdir can probably tell you more about it.

berdir’s picture

If you have a custom toArray() implementation, then this won't help you much, yes, so feel free to ignore it.

config_export can also be defined as a mapping, so if you want to use some_property in yaml and someProperty in the class, then you can do that with that now.

Question is, why exactly do you need a custom toArray() implementation?

I can see that the loop in Currency is a bit special, but CurrencyLocale doesn't seem to do anything special?

One problem with your implementation is that you also skip default functionality like dependencies, which are important for the config system. You probably don't have any, but the config system might still do something based on having a dependencies key or not..

xano’s picture

Category: Task » Bug report
Status: Needs review » Needs work

The ::toArray() override stems from when we didn't have the mappings or dependencies. We should at least make sure the parent implementation is called.

LKS90’s picture

Status: Needs work » Needs review
StatusFileSize
new6.17 KB

This patch now removes the custom code for the Currency::toArray() method and replaces it with the parent call. After the parent call we add the usages as an array instead of an object with protected fields. The second toArray method could be removed completely after some updates to the yml scheme. Mainly, the configs are now of type config_entity instead of some mapping and removed items like status and uuid which we already inherit from config_entity. I also updated the

sequence:
  - type: x
    label: y

scheme to the more consistent

sequence:
  type: x
  label: y

version.

Status: Needs review » Needs work

The last submitted patch, 6: configExport_2483965_2.patch, failed testing.

xano’s picture

Are you still working on this?

LKS90’s picture

Status: Needs work » Needs review
StatusFileSize
new8.24 KB
new2.07 KB

Updated version, expecting a test fail complaining about missing schema for exchangeRates when creating a new currency. Will fix those errors when the testbot confirms my suspicion.

Status: Needs review » Needs work

The last submitted patch, 9: add_config_export_to-2483965-9.patch, failed testing.

LKS90’s picture

Status: Needs work » Needs review
StatusFileSize
new17.52 KB
new4.46 KB

Fixed the test fails.

I'm adding some stubs for exchangeRates, just to pass tests. If I should split the changes to different issues, just tell me.

One thing that is kind of confusing: Why do I have to explicitly set the exchangeRates in the CurrencyDeleteFormWebTest but none of the other attributes?

xano’s picture

Status: Needs review » Needs work

Working with exchange rates should not have been necessary. This is a bug for which I opened #2537510: Remove traces of old historical exchange rates.

One thing that is kind of confusing: Why do I have to explicitly set the exchangeRates in the CurrencyDeleteFormWebTest but none of the other attributes?

Did you see a config error, or something else?

LKS90’s picture

StatusFileSize
new5.48 KB
new12.99 KB

Duh, I thought the exchangeRates were new.. :D
It was a schema error, the CurrencyDeleteFormWebTest creates the currency entity with Currecny::create(['currencyCode' => 'ABC',]);, all the other attributes have a default value that matches the schema, my re-added exchangeRates didn't and caused a schema error. The weird thing is I don't seem to find where the default value for those comes from when creating a currency entity directly.

Here is an updated patch that has all the exchangeRates removed, will also provide a patch for the other issue.

LKS90’s picture

Status: Needs work » Needs review
xano’s picture

  1. +++ b/src/Entity/Currency.php
    @@ -128,17 +139,6 @@ class Currency extends ConfigEntityBase implements CurrencyInterface {
    -    if (isset($values['usages'])) {
    -      $usages_data = $values['usages'];
    -      $values['usages'] = [];
    -      foreach ($usages_data as $usage_data) {
    -        $usage = new Usage();
    -        $usage->setStart($usage_data['start'])
    -          ->setEnd($usage_data['end'])
    -          ->setCountryCode($usage_data['countryCode']);
    -        $values['usages'][] = $usage;
    -      }
    -    }
    

    Why is this removed? We'll still need to convert raw usage data to Usage objects, don't we?

  2. +++ b/src/Entity/Currency.php
    @@ -398,28 +398,4 @@ class Currency extends ConfigEntityBase implements CurrencyInterface {
    -    foreach ($this->getUsages() as $usage) {
    -      $properties['usages'][] = array(
    -        'start' => $usage->getStart(),
    -        'end' => $usage->getEnd(),
    -        'countryCode' => $usage->getCountryCode(),
    -      );
    -    }
    

    Same here.

LKS90’s picture

StatusFileSize
new11.54 KB
new5.85 KB

I readded the conversion of usages to toArray() and _construct(), but as I mentioned, the unit tests for currency fail now because the parent class (ConfigEntityBase) isn't mocked. Edit: Rebase soon... :D

Status: Needs review » Needs work

The last submitted patch, 16: add_config_export_to-2483965-16.patch, failed testing.

LKS90’s picture

Status: Needs work » Needs review
StatusFileSize
new11.64 KB

Here is the rebase, completely forgot about the other mini-patch.

Status: Needs review » Needs work

The last submitted patch, 18: add_config_export_to-2483965-18.patch, failed testing.

xano’s picture

The patch contains some indentation errors (four spaces instead of two).

Parent classes can never be mocked. Methods on SUTs can be mocked, but not if it's the parent of the method you're trying to test. In this case the entity manager and typed config manager need to be mocked, as those are the dependencies ConfigEntityBase::toArray() tries to locate.

toncic’s picture

Assigned: Unassigned » toncic
Status: Needs work » Needs review

Cleaning patch and try to fix test failing, but seems that HEAD is failing. I don't providing interdiff because the previous patch was so far and I couldn't apply it.

toncic’s picture

StatusFileSize
new6.96 KB

And patch.

xano’s picture

Thanks! Can you file this as a PR at https://github.com/bartfeenstra/drupal-currency? This project no longer uses drupal.org's testing infrastructure due to its limitations, so we use another CI through Github instead.

berdir’s picture

StatusFileSize
new5.78 KB

Rebased.

berdir’s picture

StatusFileSize
new15.36 KB

Converted the two toArray() unit test methods to kernel tests, which made them so much more useful and easier to work with. And the result is the same except that we now also get some standard keys in there.

berdir’s picture

Status: Needs review » Fixed

Committed, finally.

  • Berdir committed 5724f12 on 8.x-3.x
    Issue #2483965 by LKS90, Berdir, toncic: Add config_export to Currency...

Status: Fixed » Closed (fixed)

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