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

Duplicate entry fixtures being appended to each unit test #4663

Closed
aaronbushnell opened this issue Jul 26, 2019 · 6 comments · Fixed by #4746
Closed

Duplicate entry fixtures being appended to each unit test #4663

aaronbushnell opened this issue Jul 26, 2019 · 6 comments · Fixed by #4746
Assignees
Labels
testing ✅ features related to testing

Comments

@aaronbushnell
Copy link
Contributor

Description

When I use entry fixtures in my unit tests the number of entries keeps increasing with each test ran. It appears user fixtures work correctly, though!

Here's how my tests and fixtures are setup:

tests/fixtures/data/entries.php

<?php

return [
    [
        "sectionId" => $this->sectionIds['example'],
        "typeId" => $this->typeIds['example']['example'],
        "title" => "My Entry 1"
    ],

    [
        "sectionId" => $this->sectionIds['example'],
        "typeId" => $this->typeIds['example']['example'],
        "title" => "My Entry 2"
    ],

    [
        "sectionId" => $this->sectionIds['example'],
        "typeId" => $this->typeIds['example']['example'],
        "title" => "My Entry 3"
    ]
];

tests/unit/ExampleTest.php

<?php

namespace myprojecttests;

use Codeception\Test\Unit;
use UnitTester;

use Craft;
use craft\elements\User;
use craft\elements\Entry;

use myprojecttests\fixtures\UsersFixture;
use myprojecttests\fixtures\EntriesFixture;

class ExampleTest extends Unit
{
    // Public properties
    // =========================================================================

    /**
     * @var UnitTester
     */
    protected $tester;

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

    // Tests
    // =========================================================================
    public function test1()
    {
        $this->assertEquals(3, User::find()->total()); // PASS - 3
        $this->assertEquals(3, Entry::find()->section(['example'])->total()); // PASS - 3
    }

    public function test2()
    {
        $this->assertEquals(3, User::find()->total()); // PASS - 3
        $this->assertEquals(3, Entry::find()->section(['example'])->total()); // FAIL - 6
    }

    public function test3()
    {
        $this->assertEquals(3, User::find()->total()); // PASS - 3
        $this->assertEquals(3, Entry::find()->section(['example'])->total()); // FAIL - 9
    }
}

Additional info

  • Craft version: 3.2.7
  • PHP version: 7.1.26
  • Database driver & version: MySQL 5.7.24
@aaronbushnell aaronbushnell changed the title Entry fixtures being added on each unit test Duplicate entry fixtures being appended to each unit test Jul 26, 2019
@brandonkelly brandonkelly added the testing ✅ features related to testing label Jul 26, 2019
@gtettelaar
Copy link
Contributor

@aaronbushnell Not able to reproduce this easily on my side.

Can you share your codeception.yml, unit.suite.yml and the myprojecttests\fixtures\EntriesFixture class? Also if projectConfig is enabled can you share the project.yml file?

@aaronbushnell
Copy link
Contributor Author

Sure thing! Okay if I send it to you through Discord to not publicly share the schema?

Also, I discovered something when testing this a bit further. It looks to be an issue for just one particular section that uses auto-generated titles through some custom fields. I tried disabling that and using a standard "Title" field and it worked as expected!

Not sure why that would matter but perhaps that helps here a bit?

@gtettelaar
Copy link
Contributor

gtettelaar commented Aug 7, 2019

@aaronbushnell Of course, discord is no problem!

Re the auto-generated titles. I'll have a look - but that should definitely help me get started.

@angrybrad
Copy link
Member

Not able to reproduce this, either... @gtettelaar if you need help looking into it, feel free to ping me.

gtettelaar added a commit to gtettelaar/cms that referenced this issue Aug 12, 2019
Fixes a bug where `titleFormat` was changing the title stored in the DB so the value from the fixture data file was wrong.
@gtettelaar
Copy link
Contributor

gtettelaar commented Aug 12, 2019

The problem ended up being related to titles generated through a titleFormat. If you provided a title option in your fixture data file that was different from what craft\elemements\Entry::updateTitle() would generate, Craft wouldn't be able to find the title when unloading the fixtures after the test was done.

Reason for this is because when unloading fixtures through craft\test\fixtures\elements\ElementFixture::unload() Craft tries to find the Element in the DB. To do this it calls the ElementFixture::getElement() function to find the element in the DB. getElement() uses the data from the fixture data file to build an ElementQuery. In the case of entries, it builds an EntryQuery and set's the title property to whatever is supplied from the data file.

This supplied value, however, may be wrong. The actual title as stored in the DB will be the result of rendering the titleFormat. The getElement() function will thus return nothing if the value in the data file is different from the generated title. This will cause unload() to not unload that Entry.

Fix for this is to edit craft\test\fixtures\elements\EntryFixture to take this into account. However, the data file should really also provide the correct title in the first place IMO.

@aaronbushnell
Copy link
Contributor Author

Thanks a bunch, @gtettelaar! Looks like you've got a branch started that should fix this. If you need a tester just let me know. Otherwise I'll be on the lookout when this hits in a Craft release.

Appreciate your help here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
testing ✅ features related to testing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants