Skip to content
Snippets Groups Projects
Commit fd8ee6f7 authored by David Pashaev's avatar David Pashaev Committed by Getulio Valentin Sánchez
Browse files

Issue #3013484 by gvso, davps, manuel.adan, cbeier: Serialize additional data in entity

parent 660a5bff
No related branches found
Tags 8.x-2.0-rc2
No related merge requests found
......@@ -14,14 +14,14 @@ abstract class OAuth2Manager extends BaseOAuth2Manager implements OAuth2ManagerI
/**
* The scopes to be requested.
*
* @var string
* @var string|null
*/
protected $scopes;
/**
* The end points to be requested.
*
* @var string
* @var string|null
*/
protected $endPoints;
......@@ -60,7 +60,7 @@ public function getExtraDetails($method = 'GET', $domain = NULL) {
* {@inheritdoc}
*/
public function getScopes() {
if ($this->scopes === FALSE) {
if ($this->scopes === NULL) {
$this->scopes = $this->settings->get('scopes');
}
......@@ -71,7 +71,7 @@ public function getScopes() {
* {@inheritdoc}
*/
public function getEndPoints() {
if ($this->endPoints === FALSE) {
if ($this->endPoints === NULL) {
$this->endPoints = $this->settings->get('endpoints');
}
......
......@@ -2,11 +2,11 @@
namespace Drupal\social_auth\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\social_api\Entity\SocialApi;
use function GuzzleHttp\json_encode;
/**
* Defines the Social Auth entity.
......@@ -31,14 +31,13 @@ class SocialAuth extends SocialApi implements ContentEntityInterface {
/**
* {@inheritdoc}
*/
public static function create(array $values = []) {
public static function preCreate(EntityStorageInterface $storage, array &$values) {
$additional_data = $values['additional_data'] ?? NULL;
if ($additional_data) {
$values['additional_data'] = static::serializeData($additional_data);
$values['additional_data'] = static::encode($additional_data);
}
return parent::create($values);
return parent::preCreate($storage, $values);
}
/**
......@@ -48,20 +47,20 @@ public static function create(array $values = []) {
* The user id.
*/
public function getUserId() {
return $this->get('user_id')->value;
return $this->get('user_id')->target_id;
}
/**
* Sets the additional data.
*
* @param array $data
* The serialized additional data.
* The additional data.
*
* @return \Drupal\social_auth\Entity\SocialAuth
* Drupal Social Auth Entity.
*/
public function setAdditionalData(array $data) {
$this->set('additional_data', $this->serializeData($data));
$this->set('additional_data', $this->encode($data));
return $this;
}
......@@ -70,12 +69,12 @@ public function setAdditionalData(array $data) {
* Returns the serialized additional data.
*
* @return array
* The deserialized additional data.
* The additional data.
*/
public function getAdditionalData() {
$data = $this->get('additional_data')->value;
return $this->deserializeData($data);
return $this->hasField('additional_data') && !$this->get('additional_data')->isEmpty()
? $this->decode($this->get('additional_data')->value)
: [];
}
/**
......@@ -182,7 +181,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
}
/**
* Serializes array to store in the additional data field.
* Encodes array to store in the additional data field.
*
* @param array $data
* The additional data.
......@@ -190,20 +189,20 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
* @return string
* The serialized data.
*/
protected static function serializeData(array $data) {
protected static function encode(array $data) {
return json_encode($data);
}
/**
* Deserializes string stored in the additional data field.
* Decodes string stored in the additional data field.
*
* @param string $data
* The serialized additional data.
* The encoded additional data.
*
* @return array
* The deserialized data.
* The decoded data.
*/
protected function deserializeData(string $data) {
protected function decode(string $data) {
return json_decode($data, TRUE);
}
......
<?php
namespace Drupal\Tests\social_auth\Kernel;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\social_auth\Entity\SocialAuth;
/**
* Tests social_auth entity.
*
* @group social_auth
*/
class SocialAuthEntityTest extends EntityKernelTestBase {
/**
* The social_auth entity.
*
* @var \Drupal\social_auth\Entity\SocialAuth
*/
protected $entity;
/**
* The entity storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $entityStorage;
/**
* The entity values to creation.
*
* @var array
*/
protected $values = [];
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['social_api', 'social_auth'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('social_auth');
$this->entityStorage = $this->entityTypeManager->getStorage('social_auth');
$user = $this->drupalCreateUser();
$this->values = [
'user_id' => $user->id(),
'plugin_id' => 'social_auth_provider_test',
'provider_user_id' => 'provider_id_test',
'additional_data' => ['foo' => 'bar'],
'token' => 'token_test',
];
$this->entity = $this->entityStorage->create($this->values);
$this->entity->save();
}
/**
* Tests entity creation.
*/
public function testEntityCreation() {
$entity1 = SocialAuth::create($this->values);
$entity2 = $this->entityStorage->create($this->values);
$values1 = $entity1->toArray();
$values2 = $entity2->toArray();
unset($values1['uuid'], $values2['uuid'], $values1['token'], $values2['token']);
self::assertEquals($values1, $values2);
}
/**
* Tests getter for user_id field.
*/
public function testUserId() {
self::assertEquals($this->values['user_id'], $this->entity->getUserId());
}
/**
* Tests getter/setter for additional_data field.
*/
public function testAdditionalData() {
self::assertEquals($this->values['additional_data'], $this->entity->getAdditionalData());
$new_value = [];
$this->entity->setAdditionalData($new_value);
$this->entity->save();
self::assertEquals($new_value, $this->entity->getAdditionalData());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment