You are here

public function SimpleFbConnectUserManagerTest::testSetProfilePic in Simple FB Connect 8.3

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/SimpleFbConnectUserManagerTest.php \Drupal\Tests\simple_fb_connect\Unit\SimpleFbConnectUserManagerTest::testSetProfilePic()

Tests setProfilePic when target directory is writeable.

@covers ::setProfilePic @covers ::userPictureEnabled @covers ::downloadProfilePic @covers ::getPictureDirectory

File

tests/src/Unit/SimpleFbConnectUserManagerTest.php, line 450

Class

SimpleFbConnectUserManagerTest
@coversDefaultClass Drupal\simple_fb_connect\SimpleFbConnectUserManager @group simple_fb_connect

Namespace

Drupal\Tests\simple_fb_connect\Unit

Code

public function testSetProfilePic() {
  $picture_directory = 'writeable/directory';
  $field_definition = $this
    ->getMock('Drupal\\Core\\Field\\FieldDefinitionInterface');
  $field_definition
    ->expects($this
    ->once())
    ->method('getSetting')
    ->with('file_directory')
    ->willReturn($picture_directory);
  $this->entityFieldManager
    ->expects($this
    ->any())
    ->method('getFieldDefinitions')
    ->with('user', 'user')
    ->willReturn([
    'user_picture' => $field_definition,
  ]);
  $this->token
    ->expects($this
    ->once())
    ->method('replace')
    ->willReturn($picture_directory);

  // Transliteration is called twice: first for directory and then for file.
  $this->transliteration
    ->expects($this
    ->any())
    ->method('transliterate')
    ->will($this
    ->onConsecutiveCalls($picture_directory, '12345.jpg'));

  // File object.
  $file = $this
    ->getMockBuilder('Drupal\\file\\Entity\\File')
    ->disableOriginalConstructor()
    ->getMock();
  $file
    ->expects($this
    ->once())
    ->method('id')
    ->willReturn(1);
  $storage = $this
    ->getMockBuilder('Drupal\\Core\\Entity\\EntityStorageInterface')
    ->getMock();
  $storage
    ->expects($this
    ->once())
    ->method('create')
    ->willReturn($file);

  // EntityTypeManager that will return $storage.
  $this->entityTypeManager
    ->expects($this
    ->any())
    ->method('getStorage')
    ->with('file')
    ->willReturn($storage);
  $user = $this
    ->getMockBuilder('Drupal\\user\\Entity\\User')
    ->disableOriginalConstructor()
    ->getMock();
  $this
    ->assertTrue($this->userManager
    ->setProfilePic($user, 'http://www.example.com/picture.jpg', '12345'));
}