acquia / drupal-spec-tool

A tool for specifying Drupal architecture details and generating automated tests for them.
GNU General Public License v2.0
148 stars 36 forks source link

Handle other entity types #53

Closed philippemouchel closed 2 years ago

philippemouchel commented 3 years ago

Hi there.

I'm currently using this drupal-spec-tool on a Drupal Commerce site (D9 and DC2), and I had to edit ContentModelContext file to get Behat to acknowledge Drupal Commerce entities. Here is the patch.

diff --git a/src/Context/ContentModelContext.php b/src/Context/ContentModelContext.php
index 739525f..7d50f4b 100644
--- a/src/Context/ContentModelContext.php
+++ b/src/Context/ContentModelContext.php
@@ -173,6 +173,17 @@ class ContentModelContext extends ContextBase {
   protected function getContentEntityTypes() {
     $ids = [
       'block_content',
+      'commerce_log',
+      'commerce_order',
+      'commerce_order_item',
+      'commerce_payment',
+      'commerce_payment_method',
+      'commerce_product',
+      'commerce_product_attribute_value',
+      'commerce_product_variation',
+      'commerce_promotion',
+      'commerce_promotion_coupon',
+      'commerce_store',
       'media',
       'node',
       'paragraph',

Any elegant way to do that ? How about custom entities ?

TravisCarden commented 3 years ago

Hi, @philippemouchel. Definitely!

Simply subclass ContentModelContext and override getContentEntityTypes() with your customizations. Then update your Behat configuration to use your context instead of the Spec Tool's, e.g.:

 # behat.yml
 default:
   suites:
     default:
       contexts:
-        - Acquia\DrupalSpecTool\Context\ContentModelContext
+        - AcmeCorp\MyContentModelContext

As to custom entity types, as long as they implement \Drupal\Core\Entity\ContentEntityInterface you should be fine to add basic support for them in the same place. But I haven't investigated that, and there are bound to be unanticipated effects of including them. Good luck! πŸ˜„

If you're looking for something more flexible for reuse across projects, or if you're interested in contributing to the Spec Tool itself, we could make the content entity types list configurable in behat.yml. Alternatively, we could just dynamically get all defined entity types that implement ContentEntityInterface--if that would even be desirable. If you're interested in working on something like that I may be able to give you some pointers. πŸ‘

mgreco-n commented 2 years ago

Hello! In my content_model.feature file I have the following line:

| Article (Content type) | Image | field_image | Entity reference | Required | 1 | Entity browser | | |

In the system I have implemented the field in question.

Running the test I get the following error message:

--- Missing fields (present in specification, absent from Drupal)
      | Article (Content type) | Image | field_image | Entity reference | Required | 1 | Entity browser |  |  | (TravisCarden\BehatTableComparison\UnequalTablesException)

Usually if there is something wrong with the test I can see the field in the unexpected fields list. The system appears to be unable to detect this type of field. The same thing happens with users.

In my project I also used commerce and I solved the problem by modifying the getContentEntityTypes () method as shown by philippemouchel. My system shows the error message posted below when I add the following ids in the array: 'commerce_log', 'commerce_payment_method''commerce_payment',

**The "" entity type does not exist. (Drupal\Component\Plugin\Exception\PluginNotFoundException)**
The same error occurs when I add the to the array the 'user' id in an attempt to identify system users to conduct tests.

How is it possible to resolve the situation so that we can test for the entity reference Image and for the users?

Thank you for your kind attention and for the time dedicated. I wish you a good day.

TravisCarden commented 2 years ago

@mgreco-n can you post a complete diff of the changes you made that you describe above?

Peppe87 commented 2 years ago

@TravisCarden I'm using a modified version of the patch suggested by philippemouchel.

The content_model test run correctly for commerce entities or feeds, but If I had the "user" I get too the following error.

The "" entity type does not exist. (Drupal\Component\Plugin\Exception\PluginNotFoundException)

Index: vendor/acquia/drupal-spec-tool/src/Context/ContentModelContext.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/vendor/acquia/drupal-spec-tool/src/Context/ContentModelContext.php b/vendor/acquia/drupal-spec-tool/src/Context/ContentModelContext.php
--- a/vendor/acquia/drupal-spec-tool/src/Context/ContentModelContext.php
+++ b/vendor/acquia/drupal-spec-tool/src/Context/ContentModelContext.php    (date 1649768110404)
@@ -171,13 +171,32 @@
    *   An array of entity types.
    */
   protected function getContentEntityTypes() {
+    //    $ids = [
+    //      'block_content',
+    //      'media',
+    //      'node',
+    //      'paragraph',
+    //      'taxonomy_term',
+    //    ];
+
     $ids = [
       'block_content',
+      'commerce_order',
+      'commerce_order_item',
+      'commerce_product',
+      'commerce_product_attribute_value',
+      'commerce_product_variation',
+      'commerce_promotion',
+      'commerce_promotion_coupon',
+      'commerce_store',
+      'feeds_feed',
+      'user',
       'media',
       'node',
       'paragraph',
       'taxonomy_term',
     ];
+
     $entity_types = [];
     foreach ($ids as $id) {
       try {

56-handle other entity types.patch.gz

TravisCarden commented 2 years ago

@Peppe87 are you trying to add support for user roles? That would be user_role, and there's another issue to (finish) adding support for them: https://github.com/acquia/drupal-spec-tool/issues/20.

Peppe87 commented 2 years ago

No, just to check fields on user entity.

TravisCarden commented 2 years ago

Oh... Now that you mention it, I remember looking at that ages ago, and as I recall user entities have a different API from other kinds. Why don't you create an issue for adding support for them and link to it here?

TravisCarden commented 2 years ago

I think this issue has been resolved, so I'm closing it. Re-open it if I'm mistaken.