View source
<?php
namespace Drupal\relation\Tests;
use Drupal\relation\Entity\RelationType;
use Drupal\relation\Entity\Relation;
class RelationAPITest extends RelationTestBase {
public function setUp() {
$this->sleep = TRUE;
parent::setUp();
$permissions = [
'create article content',
'create page content',
'administer relation types',
'administer relations',
'access relations',
'create relations',
'edit relations',
'delete relations',
];
$this->web_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->web_user);
}
public function testRelationHelpers() {
$exists = $this->container
->get('entity.repository.relation')
->relationExists($this->endpoints, $this->relation_type_symmetric);
$this
->verbose(print_r($exists, TRUE));
$this
->assertTrue(isset($exists[$this->relation_id_symmetric]), 'Relation exists.');
$exists = $this->container
->get('entity.repository.relation')
->relationExists($this->endpoints_4);
$this
->assertTrue(isset($exists[$this->relation_id_octopus]), 'Relation exists.');
$endpoints_do_not_exist = $this->endpoints;
$endpoints_do_not_exist[1]['entity_type'] = $this
->randomMachineName();
$this
->assertEqual(array(), $this->container
->get('entity.repository.relation')
->relationExists($endpoints_do_not_exist, $this->relation_type_symmetric), 'Relation with non-existant endpoint not found.');
$endpoints_excessive = $this->endpoints;
$endpoints_excessive[] = [
'entity_type' => $this
->randomMachineName(),
'entity_id' => 1000,
];
$this
->assertEqual(array(), $this->container
->get('entity.repository.relation')
->relationExists($endpoints_do_not_exist, $this->relation_type_symmetric), 'Relation with too many endpoints not found.');
$this
->assertEqual(array(), $this->container
->get('entity.repository.relation')
->relationExists($this->endpoints, $this
->randomMachineName()), 'Relation with invalid relation type not found.');
}
public function testRelationQuery() {
$relations = Relation::loadMultiple(array_keys(relation_query('node', $this->node1
->id())
->execute()));
$this
->assertEqual($relations[$this->relation_id_symmetric]->endpoints[1]->entity_id, $this->node4
->id(), 'Correct entity is related: ' . $relations[$this->relation_id_symmetric]->endpoints[1]->entity_id . '==' . $this->node4
->id());
$endpoints = [
[
'entity_type' => 'node',
'entity_id' => $this->node4
->id(),
],
[
'entity_type' => 'node',
'entity_id' => $this->node4
->id(),
],
];
$exists = $this->container
->get('entity.repository.relation')
->relationExists($endpoints, 'symmetric');
$this
->assertTrue(empty($exists), 'node4 is not related to node4.');
$count = count($relations);
$this
->assertEqual($count, 3);
$count = relation_query('node', $this->node4
->id())
->count()
->execute();
$this
->assertEqual($count, 6);
$count = relation_query('node', $this->node5
->id())
->count()
->execute();
$this
->assertEqual($count, 2);
$query = relation_query('node', $this->node2
->id());
$count = relation_query_add_related($query, 'node', $this->node5
->id())
->count()
->execute();
$this
->assertFalse($count);
$relations = relation_query('node', $this->node3
->id(), 1)
->execute();
$this
->assertEqual(count($relations), 3);
$this
->assertTrue(isset($relations[$this->relation_id_directional]), 'Got the correct directional relation for nid=3.');
$query = relation_query('node', $this->node2
->id());
$relations = relation_query_add_related($query, 'node', $this->node3
->id())
->execute();
$count = count($relations);
$this
->assertEqual($count, 1);
$this
->assertEqual(isset($relations[$this->relation_id_octopus]), 'Got one correct relation.');
$relations = relation_query('node', $this->node1
->id());
$or_condition = $relations
->orConditionGroup()
->condition('relation_type', $this->relation_types['directional']['id'])
->condition('relation_type', $this->relation_types['octopus']['id']);
$relations = $relations
->condition($or_condition)
->execute();
$count = count($relations);
$this
->assertEqual($count, 2);
$this
->assertTrue(isset($relations[$this->relation_id_directional]), 'Got one correct relation.');
$this
->assertTrue(isset($relations[$this->relation_id_octopus]), 'Got a second one.');
$relations = relation_query('node', $this->node1
->id())
->range(1, 2)
->sort('relation_id', 'ASC')
->execute();
$count = count($relations);
$this
->assertEqual($count, 2);
$this
->assertTrue(isset($relations[$this->relation_id_directional]), 'Got one correct relation.');
$this
->assertTrue(isset($relations[$this->relation_id_octopus]), 'Got a second one.');
$relations = relation_query('node', $this->node1
->id())
->sort('created', 'DESC')
->execute();
$this
->assertEqual($relations, [
$this->relation_id_octopus => $this->relation_id_octopus,
$this->relation_id_directional => $this->relation_id_directional,
$this->relation_id_symmetric => $this->relation_id_symmetric,
]);
for ($i = 0; $i < 10; $i++) {
$this
->createRelationSymmetric();
}
$count = relation_query('node', $this->node4
->id())
->count()
->execute();
$this
->assertEqual($count, 16);
}
public function testRelationTypes() {
$related = relation_get_related_entity('node', $this->node1
->id());
$this
->assertEqual($this->node4
->id(), $related
->id());
$related = relation_get_related_entity('node', $this->node1
->id());
$this
->assertEqual($this->node4
->id(), $related
->id());
$directional_relation_type = RelationType::load($this->relation_types['directional']['id']);
$related = relation_get_related_entity('node', $this->node3
->id(), $directional_relation_type
->id(), 1);
$this
->assertEqual($this->node1
->id(), $related
->id());
$related = relation_get_related_entity('node', $this->node3
->id(), $directional_relation_type
->id(), 0);
$this
->assertEqual($this->node4
->id(), $related
->id());
$this
->assertEqual($directional_relation_type
->label(), 'directional');
$this
->assertEqual($directional_relation_type
->reverseLabel(), 'reverse_directional');
$test_relation_type = RelationType::create([
'id' => 'test_relation_type',
]);
$test_relation_type
->save();
$this
->assertEqual($test_relation_type
->label(), 'test_relation_type');
$this
->assertEqual($test_relation_type
->reverseLabel(), 'test_relation_type');
$relation_ids = relation_query('node', $this->node4
->id())
->execute();
foreach (Relation::loadMultiple($relation_ids) as $relation) {
$relation
->delete();
}
$this
->assertFalse(relation_get_related_entity('node', $this->node4
->id()), 'The entity was not loaded after the relation was deleted.');
$available_articles = $this->container
->get('entity.repository.relation')
->getAvailable('node', 'article');
$article_labels = [];
foreach ($available_articles as $relation) {
$article_labels[] = $relation
->label();
}
$this
->assertEqual($article_labels, [
'directional',
'octopus',
'symmetric',
]);
$available_users = $this->container
->get('entity.repository.relation')
->getAvailable('user', '*');
$user_labels = [];
foreach ($available_users as $relation) {
$user_labels[] = $relation
->label();
}
$this
->assertEqual($user_labels, [
'directional_entitydifferent',
'symmetric',
]);
}
public function testRelationSave() {
foreach ($this->relation_types as $value) {
$relation_type = $value['id'];
$endpoints = $this->endpoints;
if (isset($value['min_arity'])) {
$endpoints = $value['min_arity'] == 1 ? $this->endpoints_unary : $this->endpoints_4;
}
if ($relation_type == 'directional_entitydifferent') {
$endpoints = $this->endpoints_entitydifferent;
}
$relation = Relation::create(array(
'relation_type' => $relation_type,
));
$relation->endpoints = $endpoints;
$relation
->save();
$this
->assertTrue($relation
->id(), 'Relation created.');
$count = count($relation->endpoints);
$this
->assertEqual($count, count($endpoints));
$this
->assertEqual($relation->arity->value, count($endpoints));
$this
->assertEqual($relation
->bundle(), $relation_type);
foreach ($relation->endpoints as $endpoint) {
$need_ids[$endpoint->entity_id] = TRUE;
}
foreach ($relation->endpoints as $delta => $endpoint) {
$this
->assertEqual($endpoint->entity_type, $endpoints[$delta]['entity_type'], 'The entity type is ' . $endpoints[$delta]['entity_type'] . ': ' . $endpoint->entity_type);
$this
->assertTrue(isset($need_ids[$endpoint->entity_id]), 'The entity ID is correct: ' . $need_ids[$endpoint->entity_id]);
unset($need_ids[$endpoint->entity_id]);
}
$this
->assertFalse($need_ids, 'All ids found.');
$revision = \Drupal::database()
->select('relation_revision', 'v')
->fields('v', array(
'relation_id',
))
->condition('revision_id', $relation
->getRevisionId())
->execute()
->fetchAllAssoc('relation_id');
$this
->assertTrue(array_key_exists($relation
->id(), $revision), 'Relation revision created.');
}
}
public function testRelationDelete() {
$this->node1
->delete();
$this
->assertTrue(Relation::load($this->relation_id_octopus), 'Relation is not deleted.');
$this->node2
->delete();
$this
->assertFalse(Relation::load($this->relation_id_octopus), 'Relation is deleted.');
}
public function testRelationRevision() {
}
}