Skip to content

Add unit and kernel tests for ContentIntelCollector service #8

@jjroelofs

Description

@jjroelofs

Proposal

The module currently has no automated tests. Adding tests would improve code quality, prevent regressions, and make the module more suitable for Drupal.org contribution.

Test Coverage Needed

1. Unit Tests (Mocked Dependencies)

File: tests/src/Unit/ContentIntelCollectorTest.php

  • testGetEntityTypesReturnsContentEntities()
  • testGetBundlesForEntityType()
  • testGetFieldsReturnsFieldDefinitions()
  • testLoadEntityReturnsNullForNonExistent()
  • testExtractFieldValueHandlesEntityReference()
  • testExtractFieldValueHandlesImageField()
  • testExtractFieldValueHandlesTextField()
  • testExtractFieldValueHandlesLinkField()
  • testExtractFieldValueHandlesDatetimeField()

2. Kernel Tests (Real Drupal Bootstrap)

File: tests/src/Kernel/ContentIntelCollectorTest.php

  • testListEntitiesWithPagination()
  • testListEntitiesWithBundleFilter()
  • testCollectIntelIntegration()
  • testPluginDiscovery()
  • testPluginApplicability()

3. Functional Tests (Drush Commands)

File: tests/src/Functional/DrushCommandsTest.php

  • testCiTypesCommand()
  • testCiBundlesCommand()
  • testCiFieldsCommand()
  • testCiListCommand()
  • testCiEntityCommand()
  • testCiBatchCommand()

Minimum Viable Test Suite

Priority order:

  1. Kernel test for collectIntel() - Tests the main feature
  2. Unit test for field extraction - Tests edge cases in data transformation
  3. Kernel test for plugins - Ensures plugin system works correctly

Example Test Structure

namespace Drupal\Tests\content_intel\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;

class ContentIntelCollectorTest extends KernelTestBase {

  protected static $modules = ['content_intel', 'node', 'field', 'user', 'system'];

  protected function setUp(): void {
    parent::setUp();
    $this->installEntitySchema('node');
    $this->installEntitySchema('user');
    
    NodeType::create(['type' => 'article', 'name' => 'Article'])->save();
  }

  public function testCollectIntelReturnsExpectedStructure(): void {
    $node = Node::create([
      'type' => 'article',
      'title' => 'Test Article',
    ]);
    $node->save();

    $collector = \Drupal::service('content_intel.collector');
    $intel = $collector->collectIntel($node);

    $this->assertArrayHasKey('entity', $intel);
    $this->assertArrayHasKey('fields', $intel);
    $this->assertArrayHasKey('intel', $intel);
    $this->assertEquals('article', $intel['entity']['bundle']);
  }
}

Involved Components

  • tests/src/Unit/ (NEW directory)
  • tests/src/Kernel/ (NEW directory)
  • tests/src/Functional/ (NEW directory)
  • phpunit.xml.dist (NEW - PHPUnit configuration)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions