Open Data Catalog v2.0.0
DataNodeLifeCycle2Test.php
Go to the documentation of this file.
1 <?php
2 
3 use Drupal\Component\DependencyInjection\Container;
4 use Drupal\Core\Field\FieldItemInterface;
5 use Drupal\Core\Field\FieldItemList;
6 use Drupal\Core\File\FileSystemInterface;
7 use Drupal\Core\Logger\LoggerChannel;
8 use Drupal\Core\Logger\LoggerChannelFactory;
11 use Drupal\node\Entity\Node;
12 use MockChain\Chain;
13 use MockChain\Options;
14 use MockChain\Sequence;
15 use PHPUnit\Framework\TestCase;
16 
20 class DataNodeLifeCycle2Test extends TestCase {
21 
25  public function initialSetUp(string $type, stdClass $data) {
26  $options = (new Options())
27  ->add("dkan_datastore.service", Service::class);
28  $containerChain = (new Chain($this))
29  ->add(Container::class, 'get', $options);
30  $container = $containerChain->getMock();
31 
32  $sequence = (new Sequence())
33  ->add($type)
34  ->add(json_encode($data));
35 
36  $entity = (new Chain($this))
37  ->add(Node::class, 'bundle', 'data')
38  ->add(Node::class, 'uuid', '12345')
39  ->add(Node::class, 'get', FieldItemList::class)
40  ->add(FieldItemList::class, 'first', FieldItemInterface::class)
41  ->add(FieldItemInterface::class, '__get', $sequence)
42  ->getMock();
43 
44  return [$container, $entity];
45  }
46 
50  public function testNonDistributionInsert() {
51  $metadata = (object) ['identifier' => "12345"];
52  [$container, $entity] = $this->initialSetUp('foobar', $metadata);
53 
54  \Drupal::setContainer($container);
55 
56  $cycle = new DataNodeLifeCycle($entity);
57  $this->assertNull($cycle->insert());
58  }
59 
63  public function testNonDistributionPreDelete() {
64  $metadata = (object) ['identifier' => "12345"];
65  [$container, $entity] = $this->initialSetUp('foobar', $metadata);
66 
67  \Drupal::setContainer($container);
68 
69  $cycle = new DataNodeLifeCycle($entity);
70  $this->assertNull($cycle->predelete());
71  }
72 
77  $metadata = (object) [
78  'identifier' => "12345",
79  'data' => (object) [
80  'accessURL' => "http://google.com",
81  ],
82  ];
83  [$container, $entity] = $this->initialSetUp('distribution', $metadata);
84 
85  \Drupal::setContainer($container);
86 
87  $cycle = new DataNodeLifeCycle($entity);
88  $this->assertNull($cycle->insert());
89  }
90 
94  public function testDistributionWithDownloadURL() {
95  $options = (new Options())
96  ->add("dkan_datastore.service", Service::class)
97  ->add('logger.factory', LoggerChannelFactory::class)
98  ->index(0);
99 
100  $containerChain = (new Chain($this))
101  ->add(Container::class, 'get', $options)
102  ->add(Service::class, 'import', new \Exception("Invalid metadata information or missing file information."))
103  ->add(LoggerChannelFactory::class, 'get', LoggerChannel::class)
104  ->add(LoggerChannel::class, 'log', NULL, 'log');
105 
106  $container = $containerChain->getMock();
107 
108  $metadata = (object) [
109  'identifier' => "12345",
110  'data' => (object) [
111  'downloadURL' => "http://google.com",
112  'mediaType' => "text/csv",
113  ],
114  ];
115 
116  $sequence = (new Sequence())
117  ->add('distribution')
118  ->add(json_encode($metadata));
119 
120  $entity = (new Chain($this))
121  ->add(Node::class, 'bundle', 'data')
122  ->add(Node::class, 'uuid', '12345')
123  ->add(Node::class, 'get', FieldItemList::class)
124  ->add(FieldItemList::class, 'first', FieldItemInterface::class)
125  ->add(FieldItemInterface::class, '__get', $sequence)
126  ->getMock();
127 
128  \Drupal::setContainer($container);
129 
130  $cycle = new DataNodeLifeCycle($entity);
131  $cycle->insert();
132 
133  $this->assertEquals('Invalid metadata information or missing file information.',
134  $containerChain->getStoredInput('log')[1]);
135  }
136 
140  public function testLifeCycle() {
141  $options = (new Options())
142  ->add('dkan_datastore.service', Service::class)
143  ->add('logger.factory', LoggerChannelFactory::class)
144  ->add('file_system', FileSystemInterface::class)
145  ->index(0);
146 
147  $containerChain = (new Chain($this))
148  ->add(Container::class, 'get', $options)
149  ->add(Service::class, 'import', [], 'import')
150  ->add(Service::class, 'drop', [], 'drop');
151  $container = $containerChain->getMock();
152 
153  \Drupal::setContainer($container);
154 
155  $metadata = (object) [
156  'identifier' => "12345",
157  'data' => (object) [
158  'downloadURL' => "http://google.com",
159  'mediaType' => "text/csv",
160  ],
161  ];
162 
163  $options = (new Options())
164  ->add('field_data_type', 'distribution')
165  ->add('field_json_metadata', json_encode($metadata))
166  ->use('field_get')
167  ->index(1);
168 
169  $entity = (new Chain($this))
170  ->add(Node::class, 'bundle', 'data')
171  ->add(Node::class, 'uuid', '12345')
172  ->add(Node::class, 'get', FieldItemList::class, 'field_get')
173  ->add(FieldItemList::class, 'first', FieldItemInterface::class)
174  ->add(FieldItemInterface::class, '__get', $options)
175  ->getMock();
176 
177  $cycle = new DataNodeLifeCycle($entity);
178  $cycle->insert();
179  // The right info was given to the datastore service to queue for import.
180  $this->assertEquals(['12345', TRUE], $containerChain->getStoredInput('import'));
181 
182  $cycle->predelete();
183  // The right info was given to the datastore service to drop the datastore.
184  $this->assertEquals(['12345'], $containerChain->getStoredInput('drop'));
185  }
186 
187 }
Drupal\dkan_datastore\DataNodeLifeCycle
Definition: DataNodeLifeCycle.php:11
DataNodeLifeCycle2Test\testDistributionWithDownloadURL
testDistributionWithDownloadURL()
Definition: DataNodeLifeCycle2Test.php:94
DataNodeLifeCycle2Test\initialSetUp
initialSetUp(string $type, stdClass $data)
Definition: DataNodeLifeCycle2Test.php:25
DataNodeLifeCycle2Test
Definition: DataNodeLifeCycle2Test.php:20
DataNodeLifeCycle2Test\testLifeCycle
testLifeCycle()
Definition: DataNodeLifeCycle2Test.php:140
DataNodeLifeCycle2Test\testDistributionWithoutDownloadURL
testDistributionWithoutDownloadURL()
Definition: DataNodeLifeCycle2Test.php:76
DataNodeLifeCycle2Test\testNonDistributionInsert
testNonDistributionInsert()
Definition: DataNodeLifeCycle2Test.php:50
DataNodeLifeCycle2Test\testNonDistributionPreDelete
testNonDistributionPreDelete()
Definition: DataNodeLifeCycle2Test.php:63
Drupal\dkan_datastore\Service