Commit bf4c8337 authored by Mateu Aguiló Bosch's avatar Mateu Aguiló Bosch
Browse files

Inject entity wrapper

parent 5e3de793
......@@ -7,9 +7,9 @@
namespace Drupal\typed_entity\Tests;
use Drupal\typed_entity\Entity\MockEntityWrapperService;
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\Tests\TypedEntityModules;
use Drupal\typed_entity\TypedEntity\TypedEntity;
use Drupal\typed_entity\TypedEntity\TypedEntityManager;
......@@ -78,6 +78,9 @@ class TypedEntityUnitTestCase extends \DrupalUnitTestCase {
xautoload()
->getServiceContainer()
->set('entity_wrapper_fixture_path', __DIR__ . '/fixtures/article.inc');
xautoload()
->getServiceContainer()
->set('entity_wrapper', new MockEntityWrapperService());
$typed_article = TypedEntityManager::create('node', NULL);
$this->assertTrue($typed_article instanceof Article);
......@@ -108,11 +111,6 @@ class TypedEntityUnitTestCase extends \DrupalUnitTestCase {
xautoload()
->getServiceContainer()
->set('system', $system);
$entity_wrapper = new MockEntityWrapperService();
xautoload()
->getServiceContainer()
->set('entity_wrapper', $entity_wrapper);
}
/**
......
a:3:{s:6:"bundle";s:7:"article";s:11:"entity keys";a:5:{s:2:"id";s:3:"nid";s:8:"revision";s:3:"vid";s:6:"bundle";s:4:"type";s:5:"label";s:5:"title";s:8:"language";s:8:"language";}s:6:"entity";O:8:"stdClass":31:{s:3:"vid";s:1:"2";s:3:"uid";s:1:"1";s:5:"title";s:8:"Test TOC";s:3:"log";s:0:"";s:6:"status";s:1:"1";s:7:"comment";s:1:"2";s:7:"promote";s:1:"1";s:6:"sticky";s:1:"0";s:3:"nid";s:1:"2";s:4:"type";s:7:"article";s:8:"language";s:2:"en";s:7:"created";s:10:"1380987256";s:7:"changed";s:10:"1390993936";s:4:"tnid";s:1:"0";s:9:"translate";s:1:"0";s:18:"revision_timestamp";s:10:"1390993936";s:12:"revision_uid";s:1:"1";s:4:"body";a:1:{s:3:"und";a:1:{i:0;a:5:{s:5:"value";s:1464:"<p><strong>Pellentesque habitant morbi tristique</strong> senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. <em>Aenean ultricies mi vitae est.</em> Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, <code>commodo vitae</code>, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. <a href="#">Donec non enim</a> in turpis pulvinar facilisis. Ut felis.</p><h2>Header Level 2</h2> <ol> <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit 3.</li> <li>Aliquam tincidunt mauris eu risus 2.</li></ol><blockquote><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis aliquet congue. Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero at sagittis mollis, tellus est malesuada tellus, at luctus turpis elit sit amet quam. Vivamus pretium ornare est.</p></blockquote><h3>Header Level 3</h3><ul> <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li> <li>Aliquam tincidunt mauris eu risus.</li></ul><h2 id="custom-id">This is another Header Level 2</h2><pre><code>#header h1 a { display: block; width: 300px; height: 80px; }</code></pre>";s:7:"summary";s:0:"";s:6:"format";s:9:"full_html";s:10:"safe_value";s:1410:"<p><strong>Pellentesque habitant morbi tristique</strong> senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. <em>Aenean ultricies mi vitae est.</em> Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, <code>commodo vitae</code>, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. <a href="#">Donec non enim</a> in turpis pulvinar facilisis. Ut felis.</p><h2>Header Level 2</h2><ol><li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit 3.</li><li>Aliquam tincidunt mauris eu risus 2.</li></ol><blockquote><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis aliquet congue. Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero at sagittis mollis, tellus est malesuada tellus, at luctus turpis elit sit amet quam. Vivamus pretium ornare est.</p></blockquote><h3>Header Level 3</h3><ul><li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li><li>Aliquam tincidunt mauris eu risus.</li></ul><h2 id="custom-id">This is another Header Level 2</h2><pre><code>#header h1 a { display: block; width: 300px; height: 80px; }</code></pre>";s:12:"safe_summary";s:0:"";}}}s:10:"field_tags";a:1:{s:3:"und";a:1:{i:0;a:1:{s:3:"tid";s:1:"1";}}}s:11:"field_image";a:0:{}s:18:"field_facebook_pub";a:0:{}s:9:"field_it2";a:0:{}s:11:"rdf_mapping";a:11:{s:11:"field_image";a:2:{s:10:"predicates";a:2:{i:0;s:8:"og:image";i:1;s:12:"rdfs:seeAlso";}s:4:"type";s:3:"rel";}s:10:"field_tags";a:2:{s:10:"predicates";a:1:{i:0;s:10:"dc:subject";}s:4:"type";s:3:"rel";}s:7:"rdftype";a:2:{i:0;s:9:"sioc:Item";i:1;s:13:"foaf:Document";}s:5:"title";a:1:{s:10:"predicates";a:1:{i:0;s:8:"dc:title";}}s:7:"created";a:3:{s:10:"predicates";a:2:{i:0;s:7:"dc:date";i:1;s:10:"dc:created";}s:8:"datatype";s:12:"xsd:dateTime";s:8:"callback";s:12:"date_iso8601";}s:7:"changed";a:3:{s:10:"predicates";a:1:{i:0;s:11:"dc:modified";}s:8:"datatype";s:12:"xsd:dateTime";s:8:"callback";s:12:"date_iso8601";}s:4:"body";a:1:{s:10:"predicates";a:1:{i:0;s:15:"content:encoded";}}s:3:"uid";a:2:{s:10:"predicates";a:1:{i:0;s:16:"sioc:has_creator";}s:4:"type";s:3:"rel";}s:4:"name";a:1:{s:10:"predicates";a:1:{i:0;s:9:"foaf:name";}}s:13:"comment_count";a:2:{s:10:"predicates";a:1:{i:0;s:16:"sioc:num_replies";}s:8:"datatype";s:11:"xsd:integer";}s:13:"last_activity";a:3:{s:10:"predicates";a:1:{i:0;s:23:"sioc:last_activity_date";}s:8:"datatype";s:12:"xsd:dateTime";s:8:"callback";s:12:"date_iso8601";}}s:3:"cid";s:1:"0";s:22:"last_comment_timestamp";s:10:"1380987256";s:17:"last_comment_name";N;s:16:"last_comment_uid";s:1:"1";s:13:"comment_count";s:1:"0";s:4:"name";s:5:"admin";s:7:"picture";s:1:"0";s:4:"data";s:4:"b:0;";}}
<?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;',
),
);
......@@ -41,13 +41,6 @@ class MockEntityDrupalWrapper implements MockEntityDrupalWrapperInterface {
/**
* Constructs a MockEntityDrupalWrapper.
*
* @param $type
* The type of the passed data.
* @param $data
* Optional. The entity to wrap or its identifier.
* @param $info
* Optional. Used internally to pass info about properties down the tree.
*/
public function __construct($type, $data = NULL, $info = array()) {
$this->entityType = $type;
......@@ -72,10 +65,13 @@ class MockEntityDrupalWrapper implements MockEntityDrupalWrapperInterface {
*/
public function loadFixture($fixture) {
if (!is_array($fixture)) {
if (!file_exists($fixture)) {
$fixture_path = $fixture;
$fixture = NULL;
if (!file_exists($fixture_path)) {
throw new TypedEntityException('The provided fixture file does not exist.');
}
if (!$fixture = unserialize(file_get_contents($fixture))) {
require $fixture_path;
if (empty($fixture)) {
throw new TypedEntityException('The contents of the fixture is not valid.');
}
}
......@@ -104,9 +100,7 @@ class MockEntityDrupalWrapper implements MockEntityDrupalWrapperInterface {
'entity' => $wrapper->value(),
);
$output = serialize($fixture);
$output = preg_replace("/[\n\r\t]*/", '', $output);
return $output;
return var_export($fixture, TRUE);
}
/**
......@@ -220,14 +214,14 @@ class MockEntityDrupalWrapper implements MockEntityDrupalWrapperInterface {
* {@inheritdoc}
*/
public function getIdentifier() {
return 1;
return $this->entity->{$this->entityKeys['id']};
}
/**
* {@inheritdoc}
*/
public function getBundle() {
$this->bundle;
return $this->bundle;
}
/**
......@@ -265,7 +259,7 @@ class MockEntityDrupalWrapper implements MockEntityDrupalWrapperInterface {
* {@inheritdoc}
*/
public function entityKey($name) {
return 'id';
return $this->entityKeys[$name];
}
/**
......@@ -288,7 +282,6 @@ class MockEntityDrupalWrapper implements MockEntityDrupalWrapperInterface {
*/
protected function initFixture(array $fixture) {
$this->entityKeys = $fixture['entity keys'];
$this->entityType = $fixture['type'];
$this->bundle = $fixture['bundle'];
$this->entity = $fixture['entity'];
}
......
......@@ -2,12 +2,12 @@
/**
* @file
* Contains \Drupal\typed_entity\Entity\MockEntityWrapperService.
* Contains \Drupal\typed_entity\TypedEntity\Tests\MockEntityWrapperService.
*/
namespace Drupal\typed_entity\Entity;
namespace Drupal\typed_entity\TypedEntity\Tests;
use Drupal\typed_entity\TypedEntity\Tests\MockEntityDrupalWrapper;
use Drupal\typed_entity\Entity\EntityWrapperServiceInterface;
class MockEntityWrapperService implements EntityWrapperServiceInterface {
......
......@@ -18,6 +18,7 @@ class TypedEntityModules extends AbstractExampleModules {
return array_fill_keys(array(
'system',
'xautoload',
'entity',
'typed_entity',
'typed_entity_example',
), 'module');
......@@ -47,6 +48,9 @@ class TypedEntityModules extends AbstractExampleModules {
'\\Drupal\\typed_entity_example\\Tests\\TypedEntityExampleUnitTestCase',
'\\Drupal\\typed_entity_example\\Tests\\TypedEntityExampleWebTestCase',
),
'entity' => array(
'\\EntityDrupalWrapperInterface',
),
);
}
......
......@@ -42,7 +42,12 @@ class TypedEntityManager implements TypedEntityManagerInterface {
*/
public static function getClass($entity_type, $entity) {
$classes = &drupal_static(__METHOD__);
list(,, $bundle) = entity_extract_ids($entity_type, $entity);
/** @var \EntityDrupalWrapperInterface $wrapper */
$wrapper = xautoload()
->getServiceContainer()
->get('entity_wrapper')
->wrap($entity_type, $entity);
$bundle = $wrapper->getBundle();
$cid = $entity_type . ':' . $bundle;
if (isset($classes[$cid])) {
......@@ -50,7 +55,7 @@ class TypedEntityManager implements TypedEntityManagerInterface {
}
$cached_classes = array();
if ($cache = cache_get('typed_entity_classes', 'cache_bootstrap')) {
if ($cache = static::$system->cacheGet('typed_entity_classes', 'cache_bootstrap')) {
$cached_classes = $cache->data;
}
......@@ -69,7 +74,7 @@ class TypedEntityManager implements TypedEntityManagerInterface {
break;
}
}
cache_set('typed_entity_classes', $classes, 'cache_bootstrap');
static::$system->cacheSet('typed_entity_classes', $classes, 'cache_bootstrap');
return $classes[$cid];
}
......
......@@ -11,5 +11,7 @@ use Drupal\typed_entity\Entity\EntityWrapperService;
* Implements hook_xautoload().
*/
function typed_entity_xautoload($adapter) {
xautoload()->getServiceContainer()->set('entity_wrapper', new EntityWrapperService());
xautoload()
->getServiceContainer()
->set('entity_wrapper', new EntityWrapperService());
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment