View source
<?php
namespace Drupal\Tests\radioactivity\Unit;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Site\Settings;
use Drupal\Tests\UnitTestCase;
use Drupal\radioactivity\IncidentInterface;
use Drupal\radioactivity\RestIncidentStorage;
class RestIncidentStorageTest extends UnitTestCase {
private $sut;
public function setUp() : void {
parent::setUp();
$this->sut = $this
->getMockBuilder(RestIncidentStorage::class)
->disableOriginalConstructor()
->setMethods([
'getDefaultEndpoint',
'getIncidentsFromStorage',
'clearIncidentStorage',
])
->getMock();
new Settings([
'hash_salt' => 'liesjeleerdelotjelopen',
]);
}
public function testAddIncident() {
$incident = $this
->getMockBuilder(IncidentInterface::class)
->disableOriginalConstructor()
->getMock();
$this
->expectException("Exception");
$this
->expectExceptionMessage("The Radioactivity rest endpoint expects incidents to be added somewhere else.");
$this->sut
->addIncident($incident);
}
public function testGetSingleIncident() {
$incidentData = Json::decode('[[{"fn":"field_name","et":"entity_type","id":"99","ti":"0","e":"10","h":"4bd2afb1d12a72f3a2fdb01b8fdaf128b8c09efa"}]]');
$this->sut
->expects($this
->once())
->method('getIncidentsFromStorage')
->will($this
->returnValue($incidentData));
$result = $this->sut
->getIncidents();
$this
->assertCount(1, $result);
$this
->assertInstanceOf(IncidentInterface::class, $result[0]);
}
public function testGetInvalidIncident() {
$incidentData = Json::decode('[[{"fn":"field_name","et":"entity_type","id":"99","ti":0,"e":10,"h":"invalid-hash"}]]');
$this->sut
->expects($this
->once())
->method('getIncidentsFromStorage')
->will($this
->returnValue($incidentData));
$result = $this->sut
->getIncidents();
$this
->assertCount(0, $result);
}
public function testGetMultipleIncidents() {
$this->sut
->expects($this
->once())
->method('getIncidentsFromStorage')
->will($this
->returnValue($this
->getMultipleIncidentData()));
$result = $this->sut
->getIncidents();
$this
->assertCount(4, $result);
$this
->assertInstanceOf(IncidentInterface::class, $result[0]);
}
public function testGetIncidentsByType() {
$this->sut
->expects($this
->any())
->method('getIncidentsFromStorage')
->will($this
->returnValue($this
->getMultipleIncidentData()));
$result = $this->sut
->getIncidentsByType();
$this
->assertEquals([
'entity_type',
'node',
], array_keys($result));
$this
->assertEquals([
99,
88,
], array_keys($result['entity_type']));
$this
->assertInstanceOf(IncidentInterface::class, $result['entity_type'][99][0]);
$this
->assertEquals([
123,
], array_keys($result['node']));
$this
->assertInstanceOf(IncidentInterface::class, $result['node'][123][0]);
$result = $this->sut
->getIncidentsByType('entity_type');
$this
->assertEquals([
'entity_type',
], array_keys($result));
$this
->assertEquals([
99,
88,
], array_keys($result['entity_type']));
$this
->assertInstanceOf(IncidentInterface::class, $result['entity_type'][99][0]);
$result = $this->sut
->getIncidentsByType('node');
$this
->assertEquals([
123,
], array_keys($result['node']));
$this
->assertInstanceOf(IncidentInterface::class, $result['node'][123][0]);
$result = $this->sut
->getIncidentsByType('unknown_entity');
$this
->assertArrayNotHasKey('unknown_entity', $result);
}
public function testClearIncidents() {
$this->sut
->expects($this
->once())
->method('clearIncidentStorage');
$this->sut
->clearIncidents();
}
private function getMultipleIncidentData() {
$jsonData = '[[{"fn":"field_name","et":"entity_type","id":"99","ti":"0","e":"10","h":"4bd2afb1d12a72f3a2fdb01b8fdaf128b8c09efa"}],' . PHP_EOL . '[{"fn":"field_name","et":"entity_type","id":"88","ti":0,"e":10,"h":"22c8151d3da778780c003131c64e6bc3dfd81959"}],' . PHP_EOL . '[{"fn":"field_name","et":"entity_type","id":"88","ti":0,"e":10,"h":"22c8151d3da778780c003131c64e6bc3dfd81959"}],' . PHP_EOL . '[{"fn":"field_other","et":"node","id":"123","ti":0,"e":10,"h":"0f044221c4c7a1db2b42549dfd412e5c49c44ec4"}]]';
return Json::decode($jsonData);
}
}