Skip to content
This repository was archived by the owner on Mar 16, 2021. It is now read-only.

Commit e2d80dc

Browse files
author
Mateu Aguiló Bosch
committed
Merge branch '7.x-1.x' of git.drupal.org:project/typed_entity into 7.x-1.x
2 parents 34210b6 + e17d723 commit e2d80dc

17 files changed

+1149
-104
lines changed

.travis.yml

+14-1
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,22 @@ script:
6161
- cd $TRAVIS_BUILD_DIR/../drupal
6262

6363
# Download and enable module and its dependencies
64-
- drush --yes dl xautoload
64+
- drush --yes dl entity --dev
65+
- drush --yes dl xautoload-7.x-5.x
6566
- drush --yes dl file_entity
6667

68+
# Patch xautoload to add module invoke wrapping.
69+
- cd sites/all/modules/xautoload
70+
- curl -LO https://www.drupal.org/files/issues/2456877-module-invoke-wrapping-1.patch
71+
- patch -p1 < 2456877-module-invoke-wrapping-1.patch
72+
- cd $TRAVIS_BUILD_DIR/../drupal
73+
74+
# Patch entity to add module invoke wrapping.
75+
- cd sites/all/modules/entity
76+
- curl -LO https://www.drupal.org/files/issues/2455851-add-additional-interfaces-1.patch
77+
- patch -p1 < 2455851-add-additional-interfaces-1.patch
78+
- cd $TRAVIS_BUILD_DIR/../drupal
79+
6780
# Enable the modules
6881
- drush --yes pm-enable simpletest typed_entity typed_entity_example
6982

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,12 @@ Care to add **tests**? You can even have unit testing on your custom business lo
158158
(make sure those computations on the aspect ratio return the expected values).
159159

160160
Check out the [unit test example](modules/typed_entity_example/lib/Drupal/typed_entity_example/Tests/TypedEntityExampleUnitTestCase.php).
161+
162+
## Installation
163+
This module uses unit testing as an example of how you should test your custom business logic. Sometimes your custom
164+
logic contains calls to the drupal api that is not loaded for unit testing. To work around that you can use X Autoload
165+
mock classes.
166+
167+
This module needs an extra patch to do this, so you will have to patch:
168+
- `xautoload` with: https://www.drupal.org/files/issues/2456877-module-invoke-wrapping-1.patch
169+
- `entity` with: https://www.drupal.org/files/issues/2455851-add-additional-interfaces-1.patch

lib/Drupal/typed_entity/Tests/TypedEntityUnitTestCase.php

+37-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
namespace Drupal\typed_entity\Tests;
99

1010
use Drupal\typed_entity\Exception\TypedEntityException;
11+
use Drupal\typed_entity\TypedEntity\Tests\MockEntityDrupalWrapper;
12+
use Drupal\typed_entity\TypedEntity\Tests\MockEntityWrapperService;
1113
use Drupal\typed_entity\TypedEntity\TypedEntity;
14+
use Drupal\typed_entity\TypedEntity\TypedEntityManager;
1215

1316
class TypedEntityUnitTestCase extends \DrupalUnitTestCase {
1417

@@ -43,21 +46,53 @@ public function setUp() {
4346
* Test logging message.
4447
*/
4548
public function testConstructor() {
49+
$dic = xautoload()->getServiceContainer();
4650
try {
47-
new TypedEntity(NULL, 1);
51+
new TypedEntity($dic, NULL, 1);
4852
$this->fail('Exception was not thrown for missing entity type.');
4953
}
5054
catch (TypedEntityException $e) {
5155
$this->pass('Exception was thrown for missing entity type.');
5256
}
5357

5458
try {
55-
new TypedEntity('foo');
59+
new TypedEntity($dic, 'foo');
5660
$this->fail('Exception was not thrown for missing entity and ID.');
5761
}
5862
catch (TypedEntityException $e) {
5963
$this->pass('Exception was thrown for missing entity and ID.');
6064
}
6165
}
6266

67+
/**
68+
* Test TypedEntityManager.
69+
*/
70+
public function testTypedEntityManager() {
71+
// Test the discovery.
72+
73+
// When creating the EMW the entity in the fixture will be used regardless
74+
// of the passed in entity.
75+
$wrapper_service = new MockEntityWrapperService();
76+
$wrapper_service->setFixturePath(__DIR__ . '/fixtures/article.inc');
77+
xautoload()
78+
->getServiceContainer()
79+
->set('entity_wrapper', $wrapper_service);
80+
81+
// Get the mock entity to be loaded.
82+
$entity = $wrapper_service->wrap('node', NULL)->value();
83+
$typed_article = TypedEntityManager::create('node', $entity);
84+
$this->assertEqual('node', $typed_article->getEntityType());
85+
$this->assertEqual('article', $typed_article->getBundle());
86+
$this->assertEqual($entity, $typed_article->getEntity(), 'Correct entity set');
87+
$this->assertTrue($typed_article->access('edit'));
88+
$this->assertTrue($typed_article->getWrapper() instanceof MockEntityDrupalWrapper);
89+
90+
$random_name = $this->randomName();
91+
$random_value = $this->randomString();
92+
$typed_article->{$random_name} = $random_value;
93+
$typed_article->save();
94+
$entity = $typed_article->getEntity();
95+
$this->assertEqual($entity->{$random_name}, $random_value);
96+
}
97+
6398
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php
2+
3+
$fixture = array(
4+
'bundle' => 'article',
5+
'entity keys' =>
6+
array(
7+
'id' => 'nid',
8+
'revision' => 'vid',
9+
'bundle' => 'type',
10+
'label' => 'title',
11+
'language' => 'language',
12+
),
13+
'entity' => (object) array(
14+
'vid' => '1',
15+
'uid' => '1',
16+
'title' => 'Lorem ipsum dolor sit amet, consetetur sadipscing',
17+
'log' => '',
18+
'status' => '1',
19+
'comment' => '2',
20+
'promote' => '1',
21+
'sticky' => '0',
22+
'nid' => '1',
23+
'type' => 'article',
24+
'language' => 'en',
25+
'created' => '1368005430',
26+
'changed' => '1369320916',
27+
'tnid' => '0',
28+
'translate' => '0',
29+
'revision_timestamp' => '1369320916',
30+
'revision_uid' => '1',
31+
'body' =>
32+
array(
33+
'und' =>
34+
array(
35+
0 =>
36+
array(
37+
'value' => 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
38+
'summary' => '',
39+
'format' => 'filtered_html',
40+
'safe_value' => '<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
41+
',
42+
'safe_summary' => '',
43+
),
44+
),
45+
),
46+
'field_tags' =>
47+
array(
48+
'und' =>
49+
array(
50+
0 =>
51+
array(
52+
'tid' => '1',
53+
),
54+
),
55+
),
56+
'field_image' =>
57+
array(
58+
'und' =>
59+
array(
60+
0 =>
61+
array(
62+
'fid' => '1',
63+
'uid' => '1',
64+
'filename' => 'DSCF2211.jpg',
65+
'uri' => 'public://field/image/DSCF2211.jpg',
66+
'filemime' => 'image/jpeg',
67+
'filesize' => '46156',
68+
'status' => '1',
69+
'timestamp' => '1424800819',
70+
'type' => 'image',
71+
'field_file_image_alt_text' =>
72+
array(),
73+
'field_file_image_title_text' =>
74+
array(),
75+
'rdf_mapping' =>
76+
array(),
77+
'metadata' =>
78+
array(
79+
'height' => 320,
80+
'width' => 180,
81+
),
82+
'alt' => '',
83+
'title' => '',
84+
'width' => '180',
85+
'height' => '320',
86+
),
87+
),
88+
),
89+
'field_facebook_pub' =>
90+
array(
91+
'und' =>
92+
array(
93+
0 =>
94+
array(
95+
'target_id' => '3',
96+
),
97+
),
98+
),
99+
'field_it2' =>
100+
array(),
101+
'rdf_mapping' =>
102+
array(
103+
'field_image' =>
104+
array(
105+
'predicates' =>
106+
array(
107+
0 => 'og:image',
108+
1 => 'rdfs:seeAlso',
109+
),
110+
'type' => 'rel',
111+
),
112+
'field_tags' =>
113+
array(
114+
'predicates' =>
115+
array(
116+
0 => 'dc:subject',
117+
),
118+
'type' => 'rel',
119+
),
120+
'rdftype' =>
121+
array(
122+
0 => 'sioc:Item',
123+
1 => 'foaf:Document',
124+
),
125+
'title' =>
126+
array(
127+
'predicates' =>
128+
array(
129+
0 => 'dc:title',
130+
),
131+
),
132+
'created' =>
133+
array(
134+
'predicates' =>
135+
array(
136+
0 => 'dc:date',
137+
1 => 'dc:created',
138+
),
139+
'datatype' => 'xsd:dateTime',
140+
'callback' => 'date_iso8601',
141+
),
142+
'changed' =>
143+
array(
144+
'predicates' =>
145+
array(
146+
0 => 'dc:modified',
147+
),
148+
'datatype' => 'xsd:dateTime',
149+
'callback' => 'date_iso8601',
150+
),
151+
'body' =>
152+
array(
153+
'predicates' =>
154+
array(
155+
0 => 'content:encoded',
156+
),
157+
),
158+
'uid' =>
159+
array(
160+
'predicates' =>
161+
array(
162+
0 => 'sioc:has_creator',
163+
),
164+
'type' => 'rel',
165+
),
166+
'name' =>
167+
array(
168+
'predicates' =>
169+
array(
170+
0 => 'foaf:name',
171+
),
172+
),
173+
'comment_count' =>
174+
array(
175+
'predicates' =>
176+
array(
177+
0 => 'sioc:num_replies',
178+
),
179+
'datatype' => 'xsd:integer',
180+
),
181+
'last_activity' =>
182+
array(
183+
'predicates' =>
184+
array(
185+
0 => 'sioc:last_activity_date',
186+
),
187+
'datatype' => 'xsd:dateTime',
188+
'callback' => 'date_iso8601',
189+
),
190+
),
191+
'cid' => '9',
192+
'last_comment_timestamp' => '1405499171',
193+
'last_comment_name' => '',
194+
'last_comment_uid' => '1',
195+
'comment_count' => '9',
196+
'name' => 'admin',
197+
'picture' => '0',
198+
'data' => 'b:0;',
199+
),
200+
);

modules/typed_entity_example/lib/Drupal/typed_entity_example/Tests/TypedEntityExampleUnitTestCase.php

+51-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Drupal\typed_entity_example\Tests;
99

10+
use Drupal\typed_entity\TypedEntity\Tests\MockEntityWrapperService;
1011
use Drupal\typed_entity\TypedEntity\TypedEntityManager;
1112
use Drupal\typed_entity_example\TypedEntity\Tests\TypedNodeArticleUnitTest;
1213

@@ -43,7 +44,7 @@ public function setUp() {
4344
* Test logging message.
4445
*/
4546
public function testLoggingMessage() {
46-
$typed_article = new TypedNodeArticleUnitTest('node', 1, NULL, 'article');
47+
$typed_article = new TypedNodeArticleUnitTest(xautoload()->getServiceContainer(), 'node', 1, NULL, 'article');
4748
$this->assertEqual($typed_article->getLoggingMessage(), 'User with id 1. Node with title Foo. Status 1.', 'Logging message is successful.');
4849
}
4950

@@ -56,4 +57,53 @@ public function testCamelize() {
5657
$this->assertEqual(TypedEntityManager::camelize('1-a>234'), '1A>234');
5758
$this->assertEqual(TypedEntityManager::camelize(''), '');
5859
}
60+
61+
/**
62+
* Test factory.
63+
*/
64+
public function testFactory() {
65+
$wrapper_service = new MockEntityWrapperService();
66+
$wrapper_service->setFixturePath(__DIR__ . '/fixtures/article.inc');
67+
xautoload()
68+
->getServiceContainer()
69+
->set('entity_wrapper', $wrapper_service);
70+
71+
// Get the mock entity to be loaded.
72+
$entity = $wrapper_service->wrap('node', NULL)->value();
73+
$typed_article = TypedEntityManager::create('node', $entity);
74+
$reflection_article = new \ReflectionClass($typed_article);
75+
if ($reflection_article->name == 'Drupal\typed_entity_example\TypedEntity\Node\Article') {
76+
$this->pass('The hook_typed_entity_registry_info is taking precedence.');
77+
}
78+
else {
79+
$this->fail('The hook_typed_entity_registry_info is not taking precedence.');
80+
}
81+
82+
$wrapper_service->setFixturePath(__DIR__ . '/fixtures/page.inc');
83+
// Get the mock entity to be loaded.
84+
$entity = $wrapper_service->wrap('node', NULL)->value();
85+
$typed_page = TypedEntityManager::create('node', $entity);
86+
87+
$reflection_page = new \ReflectionClass($typed_page);
88+
if ($reflection_page->name == 'Drupal\typed_entity_example\TypedEntity\TypedNode') {
89+
$this->pass('The factory is falling back to TypedNode.');
90+
}
91+
else {
92+
$this->fail('The factory is not falling back to TypedNode.');
93+
}
94+
95+
// Test the fallback to TypedEntity.
96+
$wrapper_service->setFixturePath(__DIR__ . '/fixtures/user.inc');
97+
// Get the mock entity to be loaded.
98+
$entity = $wrapper_service->wrap('user', NULL)->value();
99+
$typed_user = TypedEntityManager::create('user', $entity);
100+
$reflection_user = new \ReflectionClass($typed_user);
101+
if ($reflection_user->name == 'Drupal\typed_entity\TypedEntity\TypedEntity') {
102+
$this->pass('The factory is falling back to TypedEntity.');
103+
}
104+
else {
105+
$this->fail('The factory is not falling back to TypedEntity.');
106+
}
107+
}
108+
59109
}

0 commit comments

Comments
 (0)