|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Drupal\Tests\dkan_metastore\LifeCycle; |
| 6 | + |
| 7 | +use Drupal\dkan_common\DataResource; |
| 8 | +use Drupal\KernelTests\KernelTestBase; |
| 9 | +use RootedData\Exception\ValidationException; |
| 10 | + |
| 11 | +/** |
| 12 | + * Some tests for LifeCycle hooks. |
| 13 | + * |
| 14 | + * @group dkan |
| 15 | + * @group dkan_metastore |
| 16 | + * @group kernel |
| 17 | + * |
| 18 | + * @covers \Drupal\dkan_metastore\LifeCycle\LifeCycle |
| 19 | + * @coversDefaultClass \Drupal\dkan_metastore\LifeCycle\LifeCycle |
| 20 | + */ |
| 21 | +class LifeCycleTest extends KernelTestBase { |
| 22 | + protected const DATASET_DATA = [ |
| 23 | + 'title' => 'Test Dataset', |
| 24 | + 'identifier' => '123', |
| 25 | + 'description' => 'Test Description', |
| 26 | + 'modified' => '2026-01-01', |
| 27 | + 'accessLevel' => 'public', |
| 28 | + 'keyword' => ['test'], |
| 29 | + 'distribution' => [ |
| 30 | + [ |
| 31 | + 'title' => 'Test Distribution 1', |
| 32 | + 'downloadURL' => 'http://example.com/1.csv', |
| 33 | + ], |
| 34 | + ], |
| 35 | + "publisher" => [ |
| 36 | + "@type" => "org:Organization", |
| 37 | + "name" => "Test Org", |
| 38 | + ], |
| 39 | + "theme" => [ |
| 40 | + "Tag 1", |
| 41 | + "Tag 2", |
| 42 | + ], |
| 43 | + ]; |
| 44 | + |
| 45 | + |
| 46 | + public static $modules = [ |
| 47 | + 'system', |
| 48 | + 'node', |
| 49 | + 'user', |
| 50 | + 'field', |
| 51 | + 'filter', |
| 52 | + 'text', |
| 53 | + 'dkan_metastore', |
| 54 | + 'dkan_common', |
| 55 | + 'dkan', |
| 56 | + 'content_moderation', |
| 57 | + 'workflows', |
| 58 | + ]; |
| 59 | + |
| 60 | + /** |
| 61 | + * {@inheritdoc} |
| 62 | + */ |
| 63 | + protected function setUp(): void { |
| 64 | + parent::setUp(); |
| 65 | + $this->installConfig('system'); |
| 66 | + $this->installConfig('node'); |
| 67 | + $this->installConfig('dkan_common'); |
| 68 | + $this->installConfig('dkan_metastore'); |
| 69 | + $this->installEntitySchema('node'); |
| 70 | + $this->installSchema('node', ['node_access']); |
| 71 | + $this->installEntitySchema('content_moderation_state'); |
| 72 | + $this->installConfig('field'); |
| 73 | + $this->installEntitySchema('user'); |
| 74 | + $this->installEntitySchema('resource_mapping'); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Make sure that distributionLoad properly creates references. |
| 79 | + */ |
| 80 | + public function testDistributionLoad() { |
| 81 | + /** |
| 82 | + * @var \Drupal\dkan_metastore\MetastoreService $metastore |
| 83 | + */ |
| 84 | + $metastore = $this->container->get('dkan.metastore.service'); |
| 85 | + $metadata = $metastore->getValidMetadataFactory()->get(json_encode(self::DATASET_DATA), 'dataset'); |
| 86 | + $identifier = $metastore->post('dataset', $metadata); |
| 87 | + $result = $this->container->get('entity_type.manager') |
| 88 | + ->getStorage('node') |
| 89 | + ->loadByProperties(['type' => 'data', 'uuid' => $identifier]); |
| 90 | + $node = reset($result); |
| 91 | + |
| 92 | + // Get the raw value from the database for field_json_metadata. |
| 93 | + $query = $this->container->get('database')->query( |
| 94 | + 'SELECT field_json_metadata_value FROM {node__field_json_metadata} WHERE entity_id = :entity_id', |
| 95 | + [':entity_id' => $node->id()] |
| 96 | + ); |
| 97 | + $json_raw = $query->fetchField(); |
| 98 | + $dataset_raw = json_decode($json_raw, TRUE); |
| 99 | + $distribution_id = $dataset_raw['distribution'][0]; |
| 100 | + $result = $this->container->get('entity_type.manager') |
| 101 | + ->getStorage('node') |
| 102 | + ->loadByProperties(['type' => 'data', 'uuid' => $distribution_id]); |
| 103 | + $distribution_node = reset($result); |
| 104 | + |
| 105 | + // Get the raw value for the distribution JSON from the DB. |
| 106 | + $query = $this->container->get('database')->query( |
| 107 | + 'SELECT field_json_metadata_value FROM {node__field_json_metadata} WHERE entity_id = :entity_id', |
| 108 | + [':entity_id' => $distribution_node->id()] |
| 109 | + ); |
| 110 | + $json_raw = $query->fetchField(); |
| 111 | + $distribution_raw = json_decode($json_raw, TRUE); |
| 112 | + $download_url_ref = $distribution_raw['data']['downloadURL']; |
| 113 | + $resource_parts = DataResource::parseUniqueIdentifier($download_url_ref); |
| 114 | + |
| 115 | + // Delete the resource mapping entity. |
| 116 | + $storage = $this->container->get('entity_type.manager')->getStorage('resource_mapping'); |
| 117 | + $entities = $storage->loadByProperties([ |
| 118 | + 'identifier' => $resource_parts['identifier'], |
| 119 | + 'version' => $resource_parts['version'], |
| 120 | + 'perspective' => $resource_parts['perspective'], |
| 121 | + ]); |
| 122 | + foreach ($entities as $entity) { |
| 123 | + $entity->delete(); |
| 124 | + } |
| 125 | + |
| 126 | + // Avoid reusing the already-loaded distribution entity with a resolved URL. |
| 127 | + $this->container->get('entity_type.manager')->getStorage('node')->resetCache(); |
| 128 | + |
| 129 | + // Re-load the original dataset via the metastore service. |
| 130 | + try { |
| 131 | + $metastore->get('dataset', $identifier); |
| 132 | + $this->fail('Expected a ValidationException to be thrown due to the missing resource mapping.'); |
| 133 | + } |
| 134 | + catch (ValidationException $e) { |
| 135 | + $this->assertEquals('JSON Schema validation failed.', $e->getMessage()); |
| 136 | + } |
| 137 | + |
| 138 | + // Enable the unset_download_url_if_empty setting and try again. |
| 139 | + $config = $this->container->get('config.factory')->getEditable('dkan_metastore.settings'); |
| 140 | + $config->set('unset_download_url_if_empty', TRUE); |
| 141 | + $config->save(); |
| 142 | + $this->container->get('config.factory')->reset('dkan_metastore.settings'); |
| 143 | + $this->container->get('entity_type.manager')->getStorage('node')->resetCache(); |
| 144 | + |
| 145 | + $dataset = $metastore->get('dataset', $identifier); |
| 146 | + $this->assertArrayNotHasKey('downloadURL', $dataset->{"$.distribution[0]"}); |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments