Problem/Motivation

as requested by @alexpott in #1847280-15: Simpletest web tests misnamed as "Unit"

See also comment #11 there
"The class TermUnitTest can't be renamed, because TermTest already exists. This function should be renamed or take another action to this file fulfill the tests code standards."

Proposed resolution

make TermUnitTest a KernelTestBase by moving createTerm and createVocabulary to a trait

Remaining tasks

Contributor tasks needed
Task Novice task? Contributor instructions Complete?
Create a patch Instructions
Update the issue summary noting if allowed during the beta Instructions
Review patch to ensure that it fixes the issue, stays within scope, is properly documented, and follows coding standards Instructions

User interface changes

None.

API changes

Yes.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

martin107’s picture

Assigned: Unassigned » martin107

This looks like something worth doing ... I am working on this now.

Naming things is hard, so while I tackle the guts of the code can we talk about the name for the trait

I am frequently accused of creating variable names longer that war and peace. so my prefered name is

TaxonomyTestHelperTrait

But I am open to shorter suggestions?

martin107’s picture

Status: Active » Needs review
FileSize
5.18 KB

I am posting early and asking for help.

I have added the trait, and want to test that all the WebTests function as expected...

I am asking a question about TermUnitTest which fails locally at the moment.

To get this to work I am going to have to implement or Mock the creation of an entity type

see for example testTermDelete

$terms = entity_load_multiple_by_properties('taxonomy_term', array('vid' => $vocabulary->id()));

looking inside entity_load_multiple_by_properties I would have to mock the entityManager and the thing rapidly become a
complex unreviewable pile of spaghetti.

So the other route seems to be generating a new entity type in a unit test

Can anyone point me a similar code?

Status: Needs review » Needs work

The last submitted patch, 2: term-2431477-2.patch, failed testing.

martin107’s picture

Status: Needs work » Needs review
FileSize
451 bytes
5.22 KB

Fixed trivial thing.

Status: Needs review » Needs work

The last submitted patch, 4: term-2431477-4.patch, failed testing.

Berdir’s picture

We already have CommentTestTrait and FieldUITestTrait, so I'd just go with TaxonomyTestTrait.

Berdir’s picture

On the test fails: A kernel test is *not* a unit test, it is still simpletest. Kernel tests do not (and currently can not) mock.

The first thing that you are missing now in the test is the taxonomy_term module, because you no longer extend from TaxonomyTestBase. And next, because kernel tests don't care about module dependencies, you also need to specify text.module and probably a few others. Just keep re-running it, find out which module provides that it reports as missing and enable it. Like filter, because filter formats.

Looking at the code, it will fail on not having any filter formats, because they are not installed by default. $this->installConfig(array('filter')).

Then, after that, you need to install the database tables, because that also doesn't happen by default, for entities, that goes like this: $this->installEntitySchema('taxonomy_term') (vocabulary is a config entity and doesn't need this).

That's going to require a few definitions and lines of code. So you probably want to start a new TaxonomyKernelTestBase class that contains that and extend from that.

martin107’s picture

Status: Needs work » Needs review
FileSize
5.76 KB
2.19 KB

Thank you Bedir, I noticed this isn't the first time you have straightened out my thoughts.

Bedir++

I followed your advice and it all came out as you figured.

1) renamed TaxonomyHelperTestTrait => TaxonomyTestTrait
2) rename TaxonomyUnitTest TaxonomyKernelTest
3) added relevant modules variable and trivial lines to setUp

It may be appropriate down the road to split the two lines in the setup function into a TaxonomyKernelTestBase class but for now I have just gone with the attitude of minimal changes are often best.

Locally TermKernelTest pass

Bedir for president!

martin107’s picture

Just making notes for the next iteration.

TermKernelTest line 14 is still talking about Unit tests.

Status: Needs review » Needs work

The last submitted patch, 8: term-2431477-8.patch, failed testing.

martin107’s picture

Status: Needs work » Needs review
FileSize
5.8 KB
1.14 KB

Fixed code comment.
Fixed HelperTrait

Sorry, coding too close to bed, is never productive.

martin107’s picture

Assigned: martin107 » Unassigned

test now runs in 10 secs

Berdir’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/taxonomy/src/Tests/TaxonomyTestTrait.php
    @@ -0,0 +1,63 @@
    + * @file
    + * Definition of Drupal\taxonomy\Tests\TaxonomyTestTrait.
    

    Contains \Drupal\taxonomy... is the "new" (like two years old) standard.

  2. +++ b/core/modules/taxonomy/src/Tests/TermKernelTest.php
    @@ -2,19 +2,36 @@
      * @file
    - * Definition of Drupal\taxonomy\Tests\TermUnitTest.
    + * Definition of Drupal\taxonomy\Tests\TermKernelTest.
    

    Same. don't change files/lines you don't touch already, but we happen to touch this anyway.

  3. +++ b/core/modules/taxonomy/src/Tests/TermKernelTest.php
    @@ -2,19 +2,36 @@
    -
    +use Drupal\taxonomy\Tests\TaxonomyTestTrait;
    +use Drupal\simpletest\KernelTestBase;
     /**
    

    There should be an empty line between the last use and /**.

martin107’s picture

Status: Needs work » Needs review
FileSize
6.75 KB
2.21 KB

1, 2, and 3 fixed.

And spotted a few more nits.

Fixed the visibility of a couple of test functions.
Added a comment to testTermDelete().

Berdir’s picture

almost correct, the Contains lines should have a leading \, \Drupal\..., not Drupal\...

martin107’s picture

FileSize
6.75 KB
894 bytes

Correct, fixed.

Berdir’s picture

Status: Needs review » Reviewed & tested by the community

Great, looks good.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Test code is not frozen in beta. Committed a0e9ed1 and pushed to 8.0.x. Thanks!

  • alexpott committed a0e9ed1 on 8.0.x
    Issue #2431477 by martin107: make TermUnitTest a KernelTestBase by...

Status: Fixed » Closed (fixed)

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