You are here

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

Expanded class hierarchy of UserDataCdfAttributeTest

File

tests/src/Unit/EventSubscriber/CdfAttributes/UserDataCdfAttributeTest.php, line 24

Namespace

Drupal\Tests\acquia_contenthub\Unit\EventSubscriber\CdfAttributes
View 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

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UserDataCdfAttributeTest::$cdf private property CDF Object.
UserDataCdfAttributeTest::$dispatcher protected property Event dispatcher.
UserDataCdfAttributeTest::populateMailAttributeProvider public function Returns test user names and emails.
UserDataCdfAttributeTest::populateUserNameAttributeProvider public function Returns test user names.
UserDataCdfAttributeTest::setUp protected function Overrides UnitTestCase::setUp
UserDataCdfAttributeTest::testIsAnonymousAttributePopulation public function Tests is_anonymous attribute population.
UserDataCdfAttributeTest::testMailAttributePopulation public function Tests mail attribute population.
UserDataCdfAttributeTest::testPopulateUserNameAttribute public function Tests username attribute population.