View source
<?php
namespace Drupal\Tests\acquia_contenthub\Unit\EventSubscriber\CdfAttributes;
use Acquia\ContentHubClient\CDF\CDFObject;
use Acquia\ContentHubClient\CDFAttribute;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\Event\CdfAttributesEvent;
use Drupal\acquia_contenthub\EventSubscriber\CdfAttributes\UserDataCdfAttribute;
use Drupal\depcalc\DependentEntityWrapper;
use Drupal\Tests\UnitTestCase;
use Drupal\user\UserInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
class UserDataCdfAttributeTest extends UnitTestCase {
protected $dispatcher;
private $cdf;
protected function setUp() {
parent::setUp();
$this->cdf = $this
->getMockBuilder(CDFObject::class)
->disableOriginalConstructor()
->setMethods(NULL)
->getMock();
$this->dispatcher = new EventDispatcher();
$this->dispatcher
->addSubscriber(new UserDataCdfAttribute());
}
public function testPopulateUserNameAttribute(array $data) {
$entity = $this
->getMockBuilder(UserInterface::class)
->disableOriginalConstructor()
->setMethods([])
->getMockForAbstractClass();
$entity
->method('label')
->willReturn($data);
$entity
->method('getEntityTypeId')
->willReturn('user');
$entity
->method('uuid')
->willReturn('3f0b403c-4093-4caa-ba78-37df21125f09');
$wrapper = new DependentEntityWrapper($entity);
$event = new CdfAttributesEvent($this->cdf, $entity, $wrapper);
$this->dispatcher
->dispatch(AcquiaContentHubEvents::POPULATE_CDF_ATTRIBUTES, $event);
$attribute = $event
->getCdf()
->getAttribute('username');
$this
->assertEquals(CDFAttribute::TYPE_STRING, $attribute
->getType());
$this
->assertArrayEquals($attribute
->getValue(), [
CDFObject::LANGUAGE_UNDETERMINED => $data,
]);
}
public function testIsAnonymousAttributePopulation(array $data) {
$entity = $this
->getMockBuilder(UserInterface::class)
->disableOriginalConstructor()
->setMethods([])
->getMockForAbstractClass();
$entity
->method('label')
->willReturn($data);
$entity
->method('getEntityTypeId')
->willReturn('user');
$entity
->method('isAnonymous')
->willReturn(TRUE);
$entity
->method('uuid')
->willReturn('3f0b403c-4093-4caa-ba78-37df21125f09');
$wrapper = new DependentEntityWrapper($entity);
$event = new CdfAttributesEvent($this->cdf, $entity, $wrapper);
$this->dispatcher
->dispatch(AcquiaContentHubEvents::POPULATE_CDF_ATTRIBUTES, $event);
$attribute = $event
->getCdf()
->getAttribute('is_anonymous');
$this
->assertEquals(CDFAttribute::TYPE_BOOLEAN, $attribute
->getType());
$this
->assertArrayEquals($attribute
->getValue(), [
CDFObject::LANGUAGE_UNDETERMINED => TRUE,
]);
$this
->assertNull($event
->getCdf()
->getAttribute('mail'));
}
public function testMailAttributePopulation($user_name, $email) {
$entity = $this
->getMockBuilder(UserInterface::class)
->disableOriginalConstructor()
->setMethods([])
->getMockForAbstractClass();
$entity
->method('label')
->willReturn($user_name);
$entity
->method('getEntityTypeId')
->willReturn('user');
$entity
->method('isAnonymous')
->willReturn(FALSE);
$entity
->method('getEmail')
->willReturn($email);
$entity
->method('uuid')
->willReturn('3f0b403c-4093-4caa-ba78-37df21125f09');
$wrapper = new DependentEntityWrapper($entity);
$event = new CdfAttributesEvent($this->cdf, $entity, $wrapper);
$this->dispatcher
->dispatch(AcquiaContentHubEvents::POPULATE_CDF_ATTRIBUTES, $event);
$attribute = $event
->getCdf()
->getAttribute('mail');
$this
->assertEquals(CDFAttribute::TYPE_STRING, $attribute
->getType());
$this
->assertNull($event
->getCdf()
->getAttribute('is_anonymous'));
$this
->assertArrayEquals($attribute
->getValue(), [
CDFObject::LANGUAGE_UNDETERMINED => $email,
]);
}
public function populateUserNameAttributeProvider() {
return [
[
[
'user name',
],
],
[
[
'',
],
],
];
}
public function populateMailAttributeProvider() {
return [
[
'user name',
'example@example.com',
],
[
'',
'example@example.com',
],
];
}
}