Add basic kernel tests for ID tag field.

This commit is contained in:
Michael Stenta 2023-10-09 12:59:35 -04:00
parent d54ea7309c
commit 389786ce60
3 changed files with 99 additions and 0 deletions

View File

@ -0,0 +1,8 @@
langcode: en
status: true
dependencies: { }
id: test
label: Test
description: ''
workflow: asset_default
new_revision: true

View File

@ -0,0 +1,8 @@
name: farmOS ID Tag Test
description: Module for testing farmOS ID Tag field.
type: module
package: Testing
core_version_requirement: ^9
dependencies:
- farm:asset
- farm:farm_id_tag

View File

@ -0,0 +1,83 @@
<?php
namespace Drupal\Tests\farm_id_tag\Kernel;
use Drupal\asset\Entity\Asset;
use Drupal\KernelTests\KernelTestBase;
/**
* Test ID tag field.
*
* @group farm
*/
class IdTagTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'asset',
'farm_field',
'farm_id_tag',
'farm_id_tag_test',
'state_machine',
'user',
];
/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();
$this->installEntitySchema('asset');
$this->installConfig(['farm_id_tag', 'farm_id_tag_test']);
}
/**
* Test ID tag fields.
*/
public function testIdTagField() {
// Test creating a new asset and saving ID tag information.
$asset = Asset::create([
'name' => $this->randomString(),
'type' => 'test',
'id_tag' => [
'id' => '123456',
'type' => 'other',
'location' => 'Frame',
],
]);
$violations = $asset->validate();
$this->assertEmpty($violations);
$asset->save();
// Confirm that the asset was created with expected ID tag values.
$assets = Asset::loadMultiple();
$this->assertCount(1, $assets);
$this->assertEquals('123456', $assets[1]->get('id_tag')->id);
$this->assertEquals('other', $assets[1]->get('id_tag')->type);
$this->assertEquals('Frame', $assets[1]->get('id_tag')->location);
// Confirm that all sub-fields are optional.
$asset = Asset::create([
'name' => $this->randomString(),
'type' => 'test',
'id_tag' => [],
]);
$violations = $asset->validate();
$this->assertEmpty($violations);
$asset = Asset::create([
'name' => $this->randomString(),
'type' => 'test',
'id_tag' => [
'id' => '',
'type' => '',
'location' => '',
],
]);
$violations = $asset->validate();
$this->assertEmpty($violations);
}
}