View source
<?php
namespace Drupal\relation\Tests;
use Drupal\relation\Entity\Relation;
use Drupal\simpletest\WebTestBase;
use Drupal\relation\Entity\RelationType;
abstract class RelationTestBase extends WebTestBase {
public static $modules = [
'node',
'relation',
'relation_endpoint',
'field',
'field_ui',
'block',
'relation_dummy_field',
];
protected $sleep = FALSE;
public function setUp() {
parent::setUp();
if ($this->profile != 'standard') {
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
}
$this
->createRelationNodes();
$this
->createRelationUsers();
$this
->createRelationTypes();
$this
->createRelationEndPoints();
$this
->createRelationSymmetric();
$this
->createRelationDirectional();
$this
->createRelationOctopus();
$this
->createRelationUnary();
}
protected function createRelationNodes() {
$this->node1 = $this
->drupalCreateNode([
'type' => 'article',
'promote' => 1,
'title' => 'Grandparent',
]);
$this->node2 = $this
->drupalCreateNode([
'type' => 'article',
'promote' => 0,
]);
$this->node3 = $this
->drupalCreateNode([
'type' => 'page',
'promote' => 1,
'title' => 'Parent',
]);
$this->node4 = $this
->drupalCreateNode([
'type' => 'page',
'promote' => 0,
'title' => 'Child',
]);
$this->node5 = $this
->drupalCreateNode([
'type' => 'page',
'promote' => 0,
]);
$this->node6 = $this
->drupalCreateNode([
'type' => 'page',
'promote' => 0,
'title' => 'Unrelated',
]);
}
protected function createRelationUsers() {
$this->user1 = $this
->drupalCreateUser();
}
protected function createRelationEndPoints() {
$this->endpoints = [
[
'entity_type' => 'node',
'entity_id' => $this->node1
->id(),
],
[
'entity_type' => 'node',
'entity_id' => $this->node4
->id(),
],
];
$this->endpoints_4 = [
[
'entity_type' => 'node',
'entity_id' => $this->node1
->id(),
],
[
'entity_type' => 'node',
'entity_id' => $this->node2
->id(),
],
[
'entity_type' => 'node',
'entity_id' => $this->node3
->id(),
],
[
'entity_type' => 'node',
'entity_id' => $this->node4
->id(),
],
];
$this->endpoints_entitysame = [
[
'entity_type' => 'node',
'entity_id' => $this->node3
->id(),
],
[
'entity_type' => 'node',
'entity_id' => $this->node4
->id(),
],
];
$this->endpoints_entitydifferent = [
[
'entity_type' => 'user',
'entity_id' => $this->user1
->id(),
],
[
'entity_type' => 'node',
'entity_id' => $this->node3
->id(),
],
];
$this->endpoints_unary = [
[
'entity_type' => 'node',
'entity_id' => $this->node5
->id(),
],
];
}
protected function createRelationTypes() {
$this->relation_types['symmetric'] = [
'id' => 'symmetric',
'label' => 'symmetric',
'source_bundles' => [
'node:article',
'node:page',
'taxonomy_term:*',
'user:*',
],
];
$this->relation_types['directional'] = [
'id' => 'directional',
'label' => 'directional',
'reverse_label' => 'reverse_directional',
'directional' => TRUE,
'source_bundles' => [
'node:*',
],
'target_bundles' => [
'node:page',
],
];
$this->relation_types['directional_entitysame'] = [
'id' => 'directional_entitysame',
'label' => 'directional_entitysame',
'directional' => TRUE,
'source_bundles' => [
'node:page',
],
'target_bundles' => [
'node:page',
],
];
$this->relation_types['directional_entitydifferent'] = [
'id' => 'directional_entitydifferent',
'label' => 'directional_entitydifferent',
'directional' => TRUE,
'source_bundles' => [
'user:*',
],
'target_bundles' => [
'node:page',
],
];
$this->relation_types['octopus'] = [
'id' => 'octopus',
'label' => 'octopus',
'min_arity' => 3,
'max_arity' => 5,
'source_bundles' => [
'node:article',
'node:page',
],
];
$this->relation_types['unary'] = [
'id' => 'unary',
'label' => 'unary',
'min_arity' => 1,
'max_arity' => 1,
'source_bundles' => [
'node:page',
],
];
foreach ($this->relation_types as $values) {
$relation_type = RelationType::create($values);
$relation_type
->save();
}
}
protected function createRelationSymmetric() {
$this->relation_type_symmetric = $this->relation_types['symmetric']['id'];
$this->relation_id_symmetric = $this
->saveRelation($this->relation_type_symmetric, $this->endpoints);
}
protected function createRelationDirectional() {
$this->endpoints_directional = $this->endpoints;
$this->endpoints_directional[1]['entity_id'] = $this->node3
->id();
$this->endpoints_directional[1]['r_index'] = 1;
$this->relation_type_directional = $this->relation_types['directional']['id'];
$this->relation_id_directional = $this
->saveRelation($this->relation_type_directional, $this->endpoints_directional);
$this->endpoints_directional2 = $this->endpoints;
$this->endpoints_directional2[0]['entity_id'] = $this->node3
->id();
$this->endpoints_directional2[1]['entity_id'] = $this->node4
->id();
$this
->saveRelation($this->relation_type_directional, $this->endpoints_directional2);
$this->endpoints_entitysame[1]['r_index'] = 1;
$this->relation_type_directional_entitysame = $this->relation_types['directional_entitysame']['id'];
$this
->saveRelation($this->relation_type_directional_entitysame, $this->endpoints_entitysame);
$this->endpoints_entitysame[1]['entity_id'] = $this->node5
->id();
$this
->saveRelation($this->relation_type_directional_entitysame, $this->endpoints_entitysame);
$this->endpoints_entitysame[0]['entity_id'] = $this->node4
->id();
$this->endpoints_entitysame[1]['entity_id'] = $this->node3
->id();
$this
->saveRelation($this->relation_type_directional_entitysame, $this->endpoints_entitysame);
$this->endpoints_entitydifferent[1]['r_index'] = 1;
$this->relation_type_directional_entitydifferent = $this->relation_types['directional_entitydifferent']['id'];
$this
->saveRelation($this->relation_type_directional_entitydifferent, $this->endpoints_entitydifferent);
$this->endpoints_entitydifferent[1]['entity_id'] = $this->node4
->id();
$this
->saveRelation($this->relation_type_directional_entitydifferent, $this->endpoints_entitydifferent);
}
protected function createRelationOctopus() {
$this->relation_type_octopus = $this->relation_types['octopus']['id'];
$this->relation_id_octopus = $this
->saveRelation($this->relation_type_octopus, $this->endpoints_4);
}
protected function createRelationUnary() {
$this->relation_type_unary = $this->relation_types['unary']['id'];
$this->relation_id_unary = $this
->saveRelation($this->relation_type_unary, $this->endpoints_unary);
}
protected function saveRelation($relation_type, array $endpoints) {
$relation = Relation::create(array(
'relation_type' => $relation_type,
));
$relation->endpoints = $endpoints;
$relation
->save();
if ($this->sleep) {
sleep(1);
}
return $relation
->id();
}
}