From 38d7c912423d98eee13a2513ed2f66e21966d4fa Mon Sep 17 00:00:00 2001 From: Jonathan Hedstrom Date: Mon, 23 Mar 2015 11:47:31 -0700 Subject: [PATCH] Merge pull request #24 from ademarco/drupal-7-array-casting Drupal 7: Implement values array casting in __call() magic method Signed-off-by: Jonathan Hedstrom --- composer.json | 6 +- .../Driver/Fields/Drupal7/AbstractHandler.php | 50 +++++++++-- .../Driver/Fields/Drupal7/DatetimeHandler.php | 4 +- .../Driver/Fields/Drupal7/DefaultHandler.php | 2 +- .../Fields/Drupal7/EntityreferenceHandler.php | 2 +- .../Fields/Drupal7/LinkFieldHandler.php | 2 +- .../Driver/Fields/Drupal7/ListTextHandler.php | 2 +- .../Drupal7/TaxonomyTermReferenceHandler.php | 2 +- .../Tests/Driver/Drupal7FieldHandlerTest.php | 87 +++++++++++++++++++ .../{ => Tests}/Driver/DrushDriverTest.php | 0 .../Tests/Driver/FieldHandlerAbstractTest.php | 53 +++++++++++ 11 files changed, 193 insertions(+), 17 deletions(-) create mode 100644 tests/Drupal/Tests/Driver/Drupal7FieldHandlerTest.php rename tests/Drupal/{ => Tests}/Driver/DrushDriverTest.php (100%) create mode 100644 tests/Drupal/Tests/Driver/FieldHandlerAbstractTest.php diff --git a/composer.json b/composer.json index b63e36b7..53c258fd 100644 --- a/composer.json +++ b/composer.json @@ -17,12 +17,14 @@ }, "require-dev": { "phpspec/phpspec": "~2.0", - "phpunit/phpunit": "~4.0" + "phpunit/phpunit": "~4.0", + "mockery/mockery": "dev-master" }, "autoload": { "psr-0": { "Drupal\\Component": "src/", - "Drupal\\Driver": "src/" + "Drupal\\Driver": "src/", + "Drupal\\Tests\\Driver" : "tests/" } } } diff --git a/src/Drupal/Driver/Fields/Drupal7/AbstractHandler.php b/src/Drupal/Driver/Fields/Drupal7/AbstractHandler.php index e634c0cf..0e236021 100644 --- a/src/Drupal/Driver/Fields/Drupal7/AbstractHandler.php +++ b/src/Drupal/Driver/Fields/Drupal7/AbstractHandler.php @@ -10,14 +10,14 @@ use Drupal\Driver\Fields\FieldHandlerInterface; abstract class AbstractHandler implements FieldHandlerInterface { - /** - * @var string - */ - protected $language = LANGUAGE_NONE; /** - * @var + * @var string */ + protected $language = NULL; + protected $entity = NULL; + protected $entity_type = NULL; + protected $field_name = NULL; protected $field_info = array(); /** @@ -28,8 +28,42 @@ abstract class AbstractHandler implements FieldHandlerInterface { * @param $field_name * @return mixed */ - public function __construct($entity, $entity_type, $field_name) { - $this->field_info = field_info_field($field_name); - $this->language = field_is_translatable($entity_type, $this->field_info) ? entity_language($entity_type, $entity) : LANGUAGE_NONE; + public function __construct(\stdClass $entity, $entity_type, $field_name) { + $this->entity = $entity; + $this->entity_type = $entity_type; + $this->field_name = $field_name; + $this->field_info = $this->getFieldInfo(); + $this->language = $this->getEntityLanguage(); + } + + /** + * @param $method + * @param $args + * @return mixed + */ + public function __call($method, $args) { + if ($method == 'expand') { + $args['values'] = (array) $args['values']; + } + return call_user_func_array(array($this, $method), $args); + } + + /** + * @return bool|mixed|void + */ + public function getFieldInfo() { + return field_info_field($this->field_name); + } + + /** + * @return null|string + */ + public function getEntityLanguage() { + if (field_is_translatable($this->entity_type, $this->field_info)) { + return entity_language($this->entity_type, $this->entity); + } + else { + return LANGUAGE_NONE; + } } } diff --git a/src/Drupal/Driver/Fields/Drupal7/DatetimeHandler.php b/src/Drupal/Driver/Fields/Drupal7/DatetimeHandler.php index 52f44b3c..a8808c19 100644 --- a/src/Drupal/Driver/Fields/Drupal7/DatetimeHandler.php +++ b/src/Drupal/Driver/Fields/Drupal7/DatetimeHandler.php @@ -19,7 +19,7 @@ class DatetimeHandler extends AbstractHandler { public function expand($values) { $return = array(); if (isset($this->field_info['columns']['value2'])) { - foreach ((array)$values as $value) { + foreach ($values as $value) { $return[$this->language][] = array( 'value' => $value[0], 'value2' => $value[1], @@ -27,7 +27,7 @@ public function expand($values) { } } else { - foreach ((array)$values as $value) { + foreach ($values as $value) { $return[$this->language][] = array('value' => $value); } } diff --git a/src/Drupal/Driver/Fields/Drupal7/DefaultHandler.php b/src/Drupal/Driver/Fields/Drupal7/DefaultHandler.php index 9510a114..031598b5 100644 --- a/src/Drupal/Driver/Fields/Drupal7/DefaultHandler.php +++ b/src/Drupal/Driver/Fields/Drupal7/DefaultHandler.php @@ -18,7 +18,7 @@ class DefaultHandler extends AbstractHandler { */ public function expand($values) { $return = array(); - foreach ((array)$values as $value) { + foreach ($values as $value) { $return[$this->language][] = array('value' => $value); } return $return; diff --git a/src/Drupal/Driver/Fields/Drupal7/EntityreferenceHandler.php b/src/Drupal/Driver/Fields/Drupal7/EntityreferenceHandler.php index 06973ff0..718d161b 100644 --- a/src/Drupal/Driver/Fields/Drupal7/EntityreferenceHandler.php +++ b/src/Drupal/Driver/Fields/Drupal7/EntityreferenceHandler.php @@ -25,7 +25,7 @@ public function expand($values) { } $return = array(); - foreach ((array)$values as $value) { + foreach ($values as $value) { $target_id = db_select($entity_info['base table'], 't') ->fields('t', array($entity_info['entity keys']['id'])) ->condition('t.' . $entity_info['entity keys']['label'], $value) diff --git a/src/Drupal/Driver/Fields/Drupal7/LinkFieldHandler.php b/src/Drupal/Driver/Fields/Drupal7/LinkFieldHandler.php index d62c1277..5c14a328 100644 --- a/src/Drupal/Driver/Fields/Drupal7/LinkFieldHandler.php +++ b/src/Drupal/Driver/Fields/Drupal7/LinkFieldHandler.php @@ -18,7 +18,7 @@ class LinkFieldHandler extends AbstractHandler { */ public function expand($values) { $return = array(); - foreach ((array)$values as $value) { + foreach ($values as $value) { $return[$this->language][] = array( 'title' => $value[0], 'url' => $value[1], diff --git a/src/Drupal/Driver/Fields/Drupal7/ListTextHandler.php b/src/Drupal/Driver/Fields/Drupal7/ListTextHandler.php index f4e12a2a..ef181065 100644 --- a/src/Drupal/Driver/Fields/Drupal7/ListTextHandler.php +++ b/src/Drupal/Driver/Fields/Drupal7/ListTextHandler.php @@ -19,7 +19,7 @@ class ListTextHandler extends AbstractHandler { public function expand($values) { $return = array(); $allowed_values = array_flip($this->field_info['settings']['allowed_values']); - foreach ((array)$values as $value) { + foreach ($values as $value) { $return[$this->language][] = array('value' => $allowed_values[$value]); } return $return; diff --git a/src/Drupal/Driver/Fields/Drupal7/TaxonomyTermReferenceHandler.php b/src/Drupal/Driver/Fields/Drupal7/TaxonomyTermReferenceHandler.php index 84a18d62..64cfe42a 100644 --- a/src/Drupal/Driver/Fields/Drupal7/TaxonomyTermReferenceHandler.php +++ b/src/Drupal/Driver/Fields/Drupal7/TaxonomyTermReferenceHandler.php @@ -18,7 +18,7 @@ class TaxonomyTermReferenceHandler extends AbstractHandler { */ public function expand($values) { $return = array(); - foreach ((array)$values as $name) { + foreach ($values as $name) { $terms = taxonomy_get_term_by_name($name); if (!$terms) { throw new \Exception(sprintf("No term '%s' exists.", $name)); diff --git a/tests/Drupal/Tests/Driver/Drupal7FieldHandlerTest.php b/tests/Drupal/Tests/Driver/Drupal7FieldHandlerTest.php new file mode 100644 index 00000000..1330fb6a --- /dev/null +++ b/tests/Drupal/Tests/Driver/Drupal7FieldHandlerTest.php @@ -0,0 +1,87 @@ +getMockHandler($class_name, $entity, $entity_type, $field); + + $field_name = $field['field_name']; + $expanded_values = $handler->expand($this->values($entity->$field_name)); + $this->assertArraySubset($expected_values, $expanded_values); + } + + /** + * Data provider. + * + * @return array + */ + public function dataProvider() + { + return array( + + // Test default text field provided as simple text. + array( + 'DefaultHandler', + (object) array('field_text' => 'Text'), + 'node', + array('field_name' => 'field_text'), + array('en' => array(array('value' => 'Text'))), + ), + + // Test default text field provided as array. + array( + 'DefaultHandler', + (object) array('field_text' => array('Text')), + 'node', + array('field_name' => 'field_text'), + array('en' => array(array('value' => 'Text'))), + ), + + // Test single-value date field provided as simple text. + array( + 'DatetimeHandler', + (object) array('field_date' => '2015-01-01 00:00:00'), + 'node', + array('field_name' => 'field_date'), + array('en' => array(array('value' => '2015-01-01 00:00:00'))), + ), + + // Test single-value date field provided as an array. + array( + 'DatetimeHandler', + (object) array('field_date' => array('2015-01-01 00:00:00')), + 'node', + array('field_name' => 'field_date'), + array('en' => array(array('value' => '2015-01-01 00:00:00'))), + ), + + // Test double-value date field. Can only be provided as an array + // due to array type casting we perform in + // \Drupal\Driver\Fields\Drupal7\AbstractFieldHandler::__call() + array( + 'DatetimeHandler', + (object) array('field_date' => array(array('2015-01-01 00:00:00', '2015-01-02 00:00:00'))), + 'node', + array('field_name' => 'field_date', 'columns' => array('value' => '', 'value2' => '')), + array('en' => array(array('value' => '2015-01-01 00:00:00', 'value2' => '2015-01-02 00:00:00'))), + ), + + ); + } + +} diff --git a/tests/Drupal/Driver/DrushDriverTest.php b/tests/Drupal/Tests/Driver/DrushDriverTest.php similarity index 100% rename from tests/Drupal/Driver/DrushDriverTest.php rename to tests/Drupal/Tests/Driver/DrushDriverTest.php diff --git a/tests/Drupal/Tests/Driver/FieldHandlerAbstractTest.php b/tests/Drupal/Tests/Driver/FieldHandlerAbstractTest.php new file mode 100644 index 00000000..68484604 --- /dev/null +++ b/tests/Drupal/Tests/Driver/FieldHandlerAbstractTest.php @@ -0,0 +1,53 @@ +makePartial(); + $mock->shouldReceive('getFieldInfo')->andReturn($field); + $mock->shouldReceive('getEntityLanguage')->andReturn('en'); + $mock->__construct($entity, $entity_type, $field); + return $mock; + } + + /** + * Simulate __call() since mocked handlers will not run through magic methods. + * + * @param $values + * @return array + */ + protected function values($values) + { + return (array) $values; + } + +}