class UserDataCdfAttributeTest in Acquia Content Hub 8.2
Tests the user data cdf attribute.
@group acquia_contenthub
@package Drupal\Tests\acquia_contenthub\Unit\EventSubscriber\CdfAttributes
@covers \Drupal\acquia_contenthub\EventSubscriber\CdfAttributes\UserDataCdfAttribute
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\acquia_contenthub\Unit\EventSubscriber\CdfAttributes\UserDataCdfAttributeTest
Expanded class hierarchy of UserDataCdfAttributeTest
File
- tests/
src/ Unit/ EventSubscriber/ CdfAttributes/ UserDataCdfAttributeTest.php, line 24
Namespace
Drupal\Tests\acquia_contenthub\Unit\EventSubscriber\CdfAttributesView source
class UserDataCdfAttributeTest extends UnitTestCase {
/**
* Event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcher
*/
protected $dispatcher;
/**
* CDF Object.
*
* @var \Acquia\ContentHubClient\CDF\CDFObject
*/
private $cdf;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->cdf = $this
->getMockBuilder(CDFObject::class)
->disableOriginalConstructor()
->setMethods(NULL)
->getMock();
$this->dispatcher = new EventDispatcher();
$this->dispatcher
->addSubscriber(new UserDataCdfAttribute());
}
/**
* Tests username attribute population.
*
* @param array $data
* Data.
*
* @dataProvider populateUserNameAttributeProvider
*/
public function testPopulateUserNameAttribute(array $data) {
/** @var \Drupal\user\UserInterface $entity */
$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,
]);
}
/**
* Tests is_anonymous attribute population.
*
* @param array $data
* Data.
*
* @dataProvider populateUserNameAttributeProvider
*/
public function testIsAnonymousAttributePopulation(array $data) {
/** @var \Drupal\user\UserInterface $entity */
$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'));
}
/**
* Tests mail attribute population.
*
* @param string $user_name
* User name.
* @param string $email
* User email.
*
* @dataProvider populateMailAttributeProvider
*/
public function testMailAttributePopulation($user_name, $email) {
/** @var \Drupal\user\UserInterface $entity */
$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,
]);
}
/**
* Returns test user names.
*
* @return array
* Data sets.
*/
public function populateUserNameAttributeProvider() {
return [
[
[
'user name',
],
],
[
[
'',
],
],
];
}
/**
* Returns test user names and emails.
*
* @return array
* Data sets.
*/
public function populateMailAttributeProvider() {
return [
[
'user name',
'example@example.com',
],
[
'',
'example@example.com',
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UserDataCdfAttributeTest:: |
private | property | CDF Object. | |
UserDataCdfAttributeTest:: |
protected | property | Event dispatcher. | |
UserDataCdfAttributeTest:: |
public | function | Returns test user names and emails. | |
UserDataCdfAttributeTest:: |
public | function | Returns test user names. | |
UserDataCdfAttributeTest:: |
protected | function |
Overrides UnitTestCase:: |
|
UserDataCdfAttributeTest:: |
public | function | Tests is_anonymous attribute population. | |
UserDataCdfAttributeTest:: |
public | function | Tests mail attribute population. | |
UserDataCdfAttributeTest:: |
public | function | Tests username attribute population. |