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

Commit

Permalink
Merge branch '7.x-1.x' of git.drupal.org:project/typed_entity into 7.…
Browse files Browse the repository at this point in the history
…x-1.x
  • Loading branch information
Mateu Aguiló Bosch committed Jun 5, 2015
2 parents 34210b6 + e17d723 commit e2d80dc
Show file tree
Hide file tree
Showing 17 changed files with 1,149 additions and 104 deletions.
15 changes: 14 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,22 @@ script:
- cd $TRAVIS_BUILD_DIR/../drupal

# Download and enable module and its dependencies
- drush --yes dl xautoload
- drush --yes dl entity --dev
- drush --yes dl xautoload-7.x-5.x
- drush --yes dl file_entity

# Patch xautoload to add module invoke wrapping.
- cd sites/all/modules/xautoload
- curl -LO https://www.drupal.org/files/issues/2456877-module-invoke-wrapping-1.patch
- patch -p1 < 2456877-module-invoke-wrapping-1.patch
- cd $TRAVIS_BUILD_DIR/../drupal

# Patch entity to add module invoke wrapping.
- cd sites/all/modules/entity
- curl -LO https://www.drupal.org/files/issues/2455851-add-additional-interfaces-1.patch
- patch -p1 < 2455851-add-additional-interfaces-1.patch
- cd $TRAVIS_BUILD_DIR/../drupal

# Enable the modules
- drush --yes pm-enable simpletest typed_entity typed_entity_example

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,12 @@ Care to add **tests**? You can even have unit testing on your custom business lo
(make sure those computations on the aspect ratio return the expected values).

Check out the [unit test example](modules/typed_entity_example/lib/Drupal/typed_entity_example/Tests/TypedEntityExampleUnitTestCase.php).

## Installation
This module uses unit testing as an example of how you should test your custom business logic. Sometimes your custom
logic contains calls to the drupal api that is not loaded for unit testing. To work around that you can use X Autoload
mock classes.

This module needs an extra patch to do this, so you will have to patch:
- `xautoload` with: https://www.drupal.org/files/issues/2456877-module-invoke-wrapping-1.patch
- `entity` with: https://www.drupal.org/files/issues/2455851-add-additional-interfaces-1.patch
39 changes: 37 additions & 2 deletions lib/Drupal/typed_entity/Tests/TypedEntityUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
namespace Drupal\typed_entity\Tests;

use Drupal\typed_entity\Exception\TypedEntityException;
use Drupal\typed_entity\TypedEntity\Tests\MockEntityDrupalWrapper;
use Drupal\typed_entity\TypedEntity\Tests\MockEntityWrapperService;
use Drupal\typed_entity\TypedEntity\TypedEntity;
use Drupal\typed_entity\TypedEntity\TypedEntityManager;

class TypedEntityUnitTestCase extends \DrupalUnitTestCase {

Expand Down Expand Up @@ -43,21 +46,53 @@ public function setUp() {
* Test logging message.
*/
public function testConstructor() {
$dic = xautoload()->getServiceContainer();
try {
new TypedEntity(NULL, 1);
new TypedEntity($dic, NULL, 1);
$this->fail('Exception was not thrown for missing entity type.');
}
catch (TypedEntityException $e) {
$this->pass('Exception was thrown for missing entity type.');
}

try {
new TypedEntity('foo');
new TypedEntity($dic, 'foo');
$this->fail('Exception was not thrown for missing entity and ID.');
}
catch (TypedEntityException $e) {
$this->pass('Exception was thrown for missing entity and ID.');
}
}

/**
* Test TypedEntityManager.
*/
public function testTypedEntityManager() {
// Test the discovery.

// When creating the EMW the entity in the fixture will be used regardless
// of the passed in entity.
$wrapper_service = new MockEntityWrapperService();
$wrapper_service->setFixturePath(__DIR__ . '/fixtures/article.inc');
xautoload()
->getServiceContainer()
->set('entity_wrapper', $wrapper_service);

// Get the mock entity to be loaded.
$entity = $wrapper_service->wrap('node', NULL)->value();
$typed_article = TypedEntityManager::create('node', $entity);
$this->assertEqual('node', $typed_article->getEntityType());
$this->assertEqual('article', $typed_article->getBundle());
$this->assertEqual($entity, $typed_article->getEntity(), 'Correct entity set');
$this->assertTrue($typed_article->access('edit'));
$this->assertTrue($typed_article->getWrapper() instanceof MockEntityDrupalWrapper);

$random_name = $this->randomName();
$random_value = $this->randomString();
$typed_article->{$random_name} = $random_value;
$typed_article->save();
$entity = $typed_article->getEntity();
$this->assertEqual($entity->{$random_name}, $random_value);
}

}
200 changes: 200 additions & 0 deletions lib/Drupal/typed_entity/Tests/fixtures/article.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php

$fixture = array(
'bundle' => 'article',
'entity keys' =>
array(
'id' => 'nid',
'revision' => 'vid',
'bundle' => 'type',
'label' => 'title',
'language' => 'language',
),
'entity' => (object) array(
'vid' => '1',
'uid' => '1',
'title' => 'Lorem ipsum dolor sit amet, consetetur sadipscing',
'log' => '',
'status' => '1',
'comment' => '2',
'promote' => '1',
'sticky' => '0',
'nid' => '1',
'type' => 'article',
'language' => 'en',
'created' => '1368005430',
'changed' => '1369320916',
'tnid' => '0',
'translate' => '0',
'revision_timestamp' => '1369320916',
'revision_uid' => '1',
'body' =>
array(
'und' =>
array(
0 =>
array(
'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.',
'summary' => '',
'format' => 'filtered_html',
'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>
',
'safe_summary' => '',
),
),
),
'field_tags' =>
array(
'und' =>
array(
0 =>
array(
'tid' => '1',
),
),
),
'field_image' =>
array(
'und' =>
array(
0 =>
array(
'fid' => '1',
'uid' => '1',
'filename' => 'DSCF2211.jpg',
'uri' => 'public://field/image/DSCF2211.jpg',
'filemime' => 'image/jpeg',
'filesize' => '46156',
'status' => '1',
'timestamp' => '1424800819',
'type' => 'image',
'field_file_image_alt_text' =>
array(),
'field_file_image_title_text' =>
array(),
'rdf_mapping' =>
array(),
'metadata' =>
array(
'height' => 320,
'width' => 180,
),
'alt' => '',
'title' => '',
'width' => '180',
'height' => '320',
),
),
),
'field_facebook_pub' =>
array(
'und' =>
array(
0 =>
array(
'target_id' => '3',
),
),
),
'field_it2' =>
array(),
'rdf_mapping' =>
array(
'field_image' =>
array(
'predicates' =>
array(
0 => 'og:image',
1 => 'rdfs:seeAlso',
),
'type' => 'rel',
),
'field_tags' =>
array(
'predicates' =>
array(
0 => 'dc:subject',
),
'type' => 'rel',
),
'rdftype' =>
array(
0 => 'sioc:Item',
1 => 'foaf:Document',
),
'title' =>
array(
'predicates' =>
array(
0 => 'dc:title',
),
),
'created' =>
array(
'predicates' =>
array(
0 => 'dc:date',
1 => 'dc:created',
),
'datatype' => 'xsd:dateTime',
'callback' => 'date_iso8601',
),
'changed' =>
array(
'predicates' =>
array(
0 => 'dc:modified',
),
'datatype' => 'xsd:dateTime',
'callback' => 'date_iso8601',
),
'body' =>
array(
'predicates' =>
array(
0 => 'content:encoded',
),
),
'uid' =>
array(
'predicates' =>
array(
0 => 'sioc:has_creator',
),
'type' => 'rel',
),
'name' =>
array(
'predicates' =>
array(
0 => 'foaf:name',
),
),
'comment_count' =>
array(
'predicates' =>
array(
0 => 'sioc:num_replies',
),
'datatype' => 'xsd:integer',
),
'last_activity' =>
array(
'predicates' =>
array(
0 => 'sioc:last_activity_date',
),
'datatype' => 'xsd:dateTime',
'callback' => 'date_iso8601',
),
),
'cid' => '9',
'last_comment_timestamp' => '1405499171',
'last_comment_name' => '',
'last_comment_uid' => '1',
'comment_count' => '9',
'name' => 'admin',
'picture' => '0',
'data' => 'b:0;',
),
);
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Drupal\typed_entity_example\Tests;

use Drupal\typed_entity\TypedEntity\Tests\MockEntityWrapperService;
use Drupal\typed_entity\TypedEntity\TypedEntityManager;
use Drupal\typed_entity_example\TypedEntity\Tests\TypedNodeArticleUnitTest;

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

Expand All @@ -56,4 +57,53 @@ public function testCamelize() {
$this->assertEqual(TypedEntityManager::camelize('1-a>234'), '1A>234');
$this->assertEqual(TypedEntityManager::camelize(''), '');
}

/**
* Test factory.
*/
public function testFactory() {
$wrapper_service = new MockEntityWrapperService();
$wrapper_service->setFixturePath(__DIR__ . '/fixtures/article.inc');
xautoload()
->getServiceContainer()
->set('entity_wrapper', $wrapper_service);

// Get the mock entity to be loaded.
$entity = $wrapper_service->wrap('node', NULL)->value();
$typed_article = TypedEntityManager::create('node', $entity);
$reflection_article = new \ReflectionClass($typed_article);
if ($reflection_article->name == 'Drupal\typed_entity_example\TypedEntity\Node\Article') {
$this->pass('The hook_typed_entity_registry_info is taking precedence.');
}
else {
$this->fail('The hook_typed_entity_registry_info is not taking precedence.');
}

$wrapper_service->setFixturePath(__DIR__ . '/fixtures/page.inc');
// Get the mock entity to be loaded.
$entity = $wrapper_service->wrap('node', NULL)->value();
$typed_page = TypedEntityManager::create('node', $entity);

$reflection_page = new \ReflectionClass($typed_page);
if ($reflection_page->name == 'Drupal\typed_entity_example\TypedEntity\TypedNode') {
$this->pass('The factory is falling back to TypedNode.');
}
else {
$this->fail('The factory is not falling back to TypedNode.');
}

// Test the fallback to TypedEntity.
$wrapper_service->setFixturePath(__DIR__ . '/fixtures/user.inc');
// Get the mock entity to be loaded.
$entity = $wrapper_service->wrap('user', NULL)->value();
$typed_user = TypedEntityManager::create('user', $entity);
$reflection_user = new \ReflectionClass($typed_user);
if ($reflection_user->name == 'Drupal\typed_entity\TypedEntity\TypedEntity') {
$this->pass('The factory is falling back to TypedEntity.');
}
else {
$this->fail('The factory is not falling back to TypedEntity.');
}
}

}
Loading

0 comments on commit e2d80dc

Please sign in to comment.