Cms: [Testing] Matrix data in fixture not populating

Created on 9 Feb 2021  路  4Comments  路  Source: craftcms/cms

Description

I was trying to access data from a Matrix field (created in a fixture) in a test, but the data was coming back empty.

<?php

namespace clienttests\fixtures;

use craft\test\fixtures\elements\EntryFixture;

class EntriesFixture extends EntryFixture
{
    /**
     * @inheritdoc
     */
    public $dataFile = __DIR__ . '/data/entries.php';
}
<?php

$productTypeSettingsSection = Craft::$app->sections->getSectionByHandle('productTypeSettings');

return [
    [
        'authorId' => '1',
        'sectionId' => $productTypeSettingsSection->id,
        'typeId' => $productTypeSettingsSection->entryTypes[0]->id,
        'productType' => 'compressionSprings',
        'expandedRowTable' => [
            'new1' => [
                'type' => 'group',
                'enabled' => 1,
                'fields' => [
                    'group' => 'Heading 1',
                ],
            ],
            'new2' => [
                'type' => 'column',
                'enabled' => 1,
                'fields' => [
                    'blockAttribute' => 'material',
                ],
            ],
        ],
    ],
];
<?php

namespace clienttests\unit;

use Codeception\Test\Unit;

use UnitTester;
use Craft;
use clienttests\fixtures\EntriesFixture;

class AlgoliaTest extends Unit
{
    /**
     * @var UnitTester
     */
    protected $tester;

    public function _fixtures(): array
    {
        return [
            'entries' => [
                'class' => EntriesFixture::class,
            ],
        ];
    }

    public function testFormatExpandedRowSettings()
    {
        $entry = \craft\elements\Entry::findOne([
            'section' => 'productTypeSettings',
            'with' => [
                'expandedRowTable',
            ],
        ]);

        codecept_debug($entry->expandedRowTable);
    }
}

The entry definitely exists, but the contents of the Matrix field is empty.

Steps to reproduce

  1. Create fixture data that populates a matrix field (like this example in the docs)
  2. Add test which accesses the data from the matrix
  3. See matrix data is empty

Additional info

  • Craft version: Craft Pro 3.6.2
  • PHP version: 7.4.12
  • Database driver & version: MySQL 5.7.29
  • Plugins & versions:
Asset Rev 6.0.2
Classnames 1.0.2
Command Palette 3.1.4
Craft Commerce 3.2.14.1
Feed Me 4.3.4
Linkit 1.1.12.1
Ray 1.0.1
Redactor 2.8.5
Scout 2.3.1
bug

All 4 comments

I think this is actually moot. I looked at the fixtures y'all had in the repo and noticed that the custom fields are now prefixed with field:

https://github.com/craftcms/cms/blob/cb4367c0867ea2bd3ad0b503eb9affb537149e7e/tests/fixtures/data/entry-with-fields.php#L9-L11

Is there already a pending update of the docs? Otherwise I'm happy to open a PR. Thanks!

I鈥檓 on the case, @davist11. Thanks for pointing this out!

Lemme know if that fixed it for you @davist11 and I'll close this one out, otherwise can dig in more.

馃檹

Yep it sure did, thanks.

Was this page helpful?
0 / 5 - 0 ratings