forked from mapasculturais/mapasculturais
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaxonomiesTest.php
More file actions
119 lines (85 loc) · 4.12 KB
/
Copy pathTaxonomiesTest.php
File metadata and controls
119 lines (85 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
require_once 'bootstrap.php';
use MapasCulturais\App;
use MapasCulturais\Entities\Agent;
class TaxonomiesTests extends MapasCulturais_TestCase{
protected function _registerTaxonomy(){
$app = App::i();
$taxonomies = [
'test_1' => 'Test Taxonomy 1',
'test_2' => 'Test Taxonomy 2',
];
$id = 10;
foreach ($taxonomies as $slug => $description){
$id++;
$def = new \MapasCulturais\Definitions\Taxonomy($id, $slug, $description);
$app->registerTaxonomy('MapasCulturais\Entities\Agent', $def);
}
}
function _getAgentById($id){
$app = App::i();
$app->em->clear();
return $app->repo('Agent')->find($id);
}
function testPersistence(){
$this->_registerTaxonomy();
$this->user = 'normal';
$terms_1 = [
'test_1' => ['test 1', 'test 2', 'test 3'],
'tag' => ['tag 1', 'tag 2'],
'not_registered' => ['not 1', 'not 2']
];
$terms_2 = [
'test_1' => ['test 2', 'test 3', 'test 4'],
'test_2' => ['test 21', 'test 22', 'test 23'],
'tag' => ['tag 2', 'tag 3'],
'not_registered' => ['not 1', 'not 2']
];
$terms_3 = [
'test_1' => ['test 2', 'test 3', 'test 4']
];
$agent = $this->getNewEntity('Agent');
$agent->terms = $terms_1;
$agent->save(true);
$agent = $this->_getAgentById($agent->id);
$this->assertEquals($terms_1['test_1'], $agent->terms['test_1'], 'Asserting that registered taxonomy terms was saved');
$this->assertEquals($terms_1['tag'], $agent->terms['tag'], 'Asserting that registered taxonomy terms was saved');
$agent->terms = $terms_2;
$agent->save(true);
$agent = $this->_getAgentById($agent->id);
$this->assertEquals($terms_2['test_1'], $agent->terms['test_1'], 'Asserting that registered taxonomy terms was saved');
$this->assertEquals($terms_2['tag'], $agent->terms['tag'], 'Asserting that registered taxonomy terms was saved');
}
function testNotRegisteredTaxonomies(){
$this->_registerTaxonomy();
$this->user = 'normal';
$app = App::i();
$conn = $app->em->getConnection();
$terms_1 = [
'test_1' => ['test 1', 'test 2', 'test 3'],
'tag' => ['tag 1', 'tag 2'],
'not_registered' => ['not 1', 'not 2']
];
$terms_2 = [
'test_1' => ['test 2', 'test 3', 'test 4'],
'test_2' => ['test 21', 'test 22', 'test 23'],
'tag' => ['tag 2', 'tag 3'],
'not_registered' => ['not 1', 'not 2']
];
$agent = $this->getNewEntity('Agent');
$agent->terms = $terms_1;
$agent->save(true);
$agent = $this->_getAgentById($agent->id);
$this->assertArrayNotHasKey('not_registered', $agent->terms, 'Asserting that not registered taxonomy terms was not saved');
$conn->executeQuery("INSERT INTO term(id,taxonomy,term) VALUES (10000,'not_registered','not registered 1')");
$conn->executeQuery("INSERT INTO term(id,taxonomy,term) VALUES (10001,'not_registered','not registered 2')");
$conn->executeQuery("INSERT INTO term_relation(term_id,object_type,object_id) VALUES (10000,'MapasCulturais\Entities\Agent',{$agent->id})");
$conn->executeQuery("INSERT INTO term_relation(term_id,object_type,object_id) VALUES (10001,'MapasCulturais\Entities\Agent',{$agent->id})");
$agent = $this->_getAgentById($agent->id);
$agent->terms = $terms_2;
$agent->save(true);
$agent = $this->_getAgentById($agent->id);
$trs = $conn->fetchAll('SELECT * FROM term_relation WHERE term_id IN (10000,10001)');
$this->assertEquals(2,count($trs), 'Asserting that not registered taxonomy terms was not deleted after save terms');
}
}