Ejemplo n.º 1
0
 /**
  * Tests upload() metadata sanitisation.
  *
  * @covers ::login
  * @covers ::upload
  * @covers ::doRequest
  *
  * @test
  */
 public function uploadCorrectlySantitisesMetadata()
 {
     $mock_login_response = $this->getMockBuilder('\\GuzzleHttp\\Psr7\\Response')->disableOriginalConstructor()->getMock();
     $mock_login_response->expects($this->once())->method('getStatusCode')->willReturn(200);
     $mock_login_response->expects($this->once())->method('getBody')->willReturn(file_get_contents('expected/login-expected-good-response.json', TRUE));
     // This sucks, returnValueMap wasn't working though.
     $this->client->expects($this->at(0))->method('request')->with('POST', self::EXAMPLE_LOGIN_URL, $this->defaultLoginOptions)->willReturn($mock_login_response);
     $mock_upload_response = $this->getMockBuilder('\\GuzzleHttp\\Psr7\\Response')->disableOriginalConstructor()->getMock();
     $mock_upload_response->expects($this->once())->method('getStatusCode')->willReturn(200);
     $mock_upload_response->expects($this->once())->method('getBody')->willReturn(file_get_contents('expected/upload-expected-good-response.json', TRUE));
     $expected_realpath = dirname(__FILE__) . '/expected/cat3.png';
     $mock_sourcepath = 'public://test123';
     $expected_filename = 'cat3.png';
     $unsanitised_metadata = ['is_string' => 'abc', 'is_integer' => 123, 'is_array' => ['a' => 1, 'b' => 2], 'is_object' => (object) ['a' => 1, 'b' => 2], 'libraries' => 101];
     $json_values_expected = ['description' => $expected_filename, 'is_string' => 'abc', 'is_integer' => 123, 'libraries' => '101'];
     $body = ['multipart' => [['name' => 'jsonrequest', 'contents' => $this->serializer->encode($json_values_expected)], ['name' => 'file', 'contents' => file_get_contents($expected_realpath), 'filename' => $expected_filename]]];
     $options = $this->defaultOptions + $body;
     $this->fileSystem->expects($this->once())->method('realpath')->with($mock_sourcepath)->willReturn($expected_realpath);
     $this->client->expects($this->at(1))->method('request')->with('POST', self::EXAMPLE_UPLOAD_URL, $options)->willReturn($mock_upload_response);
     /** @var \Drupal\embridge\EmbridgeAssetEntityInterface|\PHPUnit_Framework_MockObject_MockObject $mock_asset */
     $mock_asset = $this->getMockBuilder('\\Drupal\\embridge\\EmbridgeAssetEntityInterface')->disableOriginalConstructor()->getMock();
     $mock_asset->expects($this->once())->method('getSourcePath')->willReturn($mock_sourcepath);
     $mock_asset->expects($this->once())->method('getFileName')->willReturn($expected_filename);
     $this->emdbClient->upload($mock_asset, $unsanitised_metadata);
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function upload(EmbridgeAssetEntityInterface $asset, array $metadata = [])
 {
     $this->login();
     $file_path = $this->fileSystem->realpath($asset->getSourcePath());
     $filename = $asset->getFilename();
     // Sanitize metadata values.
     $metadata = array_filter($metadata, 'is_scalar');
     if (isset($metadata['libraries'])) {
         // The libraries value won't stick unless it is a string.
         $metadata['libraries'] = (string) $metadata['libraries'];
     }
     // Build the main request data.
     $json_values = ['description' => $filename];
     $json_values = array_merge($json_values, $metadata);
     $json_request = $this->jsonEncoder->encode($json_values);
     $body = ['multipart' => [['name' => 'jsonrequest', 'contents' => $json_request], ['name' => 'file', 'contents' => file_get_contents($file_path), 'filename' => $filename]]];
     $response_body = $this->doRequest(self::EMBRIDGE_UPLOAD_PATH_DEFAULT, $body);
     $asset->setAssetId($response_body['data']['id']);
     $asset->setSourcePath($response_body['data']['sourcepath']);
     return $asset;
 }
Ejemplo n.º 3
0
 /**
  * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::setIfNotExists().
  */
 public function setIfNotExists($key, $value)
 {
     $result = $this->connection->merge($this->table)->insertFields(array('collection' => $this->collection, 'name' => $key, 'value' => $this->serializer->encode($value)))->condition('collection', $this->collection)->condition('name', $key)->execute();
     return $result == Merge::STATUS_INSERT;
 }
Ejemplo n.º 4
0
 public static function encode($data)
 {
     return static::$mock->encode($data);
 }