/**
  * Sets up all of our services with mock methods so buildForm() is possible.
  *
  * @param string $catalog_id
  *   Catalog id.
  * @param string $application_id
  *   Application id.
  * @param [] $filters
  *   An optional list of filters for the client to receive.
  */
 protected function baseMockBuild($catalog_id, $application_id, $filters = [])
 {
     // Field manager mocking.
     $field_settings = ['max_filesize' => '2 MB', 'file_extensions' => self::MOCK_FIELD_SETTINGS_FILE_EXTENSIONS, 'catalog_id' => $catalog_id];
     $this->fieldConfig->expects($this->once())->method('getName')->willReturn('field_test');
     $this->fieldConfig->expects($this->once())->method('getSettings')->willReturn($field_settings);
     // Search filter always starts with the extensions OR filter.
     $extension_filter_value = str_replace(',', '|', self::MOCK_FIELD_SETTINGS_FILE_EXTENSIONS);
     $extension_filter = ['field' => 'fileformat', 'operator' => 'matches', 'value' => $extension_filter_value];
     array_unshift($filters, $extension_filter);
     // Client mocking.
     $search_response = $this->json->decode(file_get_contents('expected/search-expected-small-response.json', TRUE));
     $per_page = 8;
     $page = 1;
     $this->client->expects($this->once())->method('search')->with($page, $per_page, $filters)->willReturn($search_response);
     // Entity type storage mocking.
     $mock_catalog = $this->getMockBuilder('\\Drupal\\embridge\\EmbridgeCatalogInterface')->disableOriginalConstructor()->getMock();
     $mock_catalog->expects($this->once())->method('getApplicationId')->willReturn($application_id);
     $mock_catalog_storage = $this->getMock(EntityStorageInterface::class);
     $mock_catalog_storage->expects($this->once())->method('load')->with($catalog_id)->willReturn($mock_catalog);
     $this->entityTypeManager->expects($this->once())->method('getStorage')->with('embridge_catalog')->willReturn($mock_catalog_storage);
     // Create mock assets.
     foreach ($search_response['results'] as $i => $result) {
         $mock_asset = $this->getMockBuilder('\\Drupal\\embridge\\EmbridgeAssetEntityInterface')->disableOriginalConstructor()->getMock();
         $mock_asset->expects($this->once())->method('id')->willReturn($i);
         $this->mockAssets[$i]['asset'] = $mock_asset;
         $this->mockAssets[$i]['result'] = $result;
     }
     // Mock up the asset helper.
     $return_map = [];
     foreach ($this->mockAssets as $id => $asset_result) {
         $return_map[] = [$asset_result['result'], $catalog_id, $asset_result['asset']];
     }
     $this->assetHelper->expects($this->exactly(count($this->mockAssets)))->method('searchResultToAsset')->will($this->returnValueMap($return_map));
 }
 /**
  * Tests deleteTemporaryAssets() when the config item for age is 0.
  *
  * @covers ::deleteTemporaryAssets
  *
  * @test
  */
 public function deleteTemporaryAssetsDeletesAssetsWhenAgeIsGreaterThanZero()
 {
     $mock_config = $this->getMockBuilder(ImmutableConfig::class)->disableOriginalConstructor()->getMock();
     $age = 123456;
     $mock_config->expects($this->once())->method('get')->with('temporary_maximum_age')->willReturn($age);
     $this->configFactory->expects($this->once())->method('get')->with('system.file')->willReturn($mock_config);
     $mock_query = $this->getMock('\\Drupal\\Core\\Entity\\Query\\QueryInterface');
     $map = [['status', FILE_STATUS_PERMANENT, '<>'], ['changed', MOCK_TIMESTAMP - $age, '<']];
     $mock_query->expects($this->exactly(2))->method('condition')->will($this->returnValueMap($map));
     $mock_query->expects($this->once())->method('range')->with(0, 50)->will($this->returnSelf());
     $mock_asset_ids = [123, 456, 789];
     $mock_asset_ids = array_combine($mock_asset_ids, $mock_asset_ids);
     $mock_query->expects($this->once())->method('execute')->willReturn($mock_asset_ids);
     $mock_entity_storage = $this->getMock(EntityStorageInterface::class);
     $mock_entity_storage->expects($this->once())->method('getQuery')->willReturn($mock_query);
     $mock_assets = [];
     $log_map = [];
     foreach ($mock_asset_ids as $id) {
         $mock_asset = $this->getMockBuilder('\\Drupal\\embridge\\EmbridgeAssetEntityInterface')->disableOriginalConstructor()->getMock();
         $mock_asset->expects($this->once())->method('delete');
         $filename = 'Mock Asset ' . $id;
         $mock_asset->expects($this->once())->method('getFilename')->willReturn($filename);
         $mock_asset->expects($this->once())->method('id')->willReturn($id);
         $log_map[] = ['Embridge Asset "%filename" [%id] garbage collected during cron.', ['%filename' => $filename, '%id' => $id]];
         $mock_assets[$id] = $mock_asset;
     }
     $this->logger->expects($this->exactly(count($mock_asset_ids)))->method('notice')->will($this->returnValueMap($log_map));
     $mock_entity_storage->expects($this->once())->method('loadMultiple')->with($mock_asset_ids)->willReturn($mock_assets);
     $this->entityTypeManager->expects($this->once())->method('getStorage')->with('embridge_asset_entity')->willReturn($mock_entity_storage);
     $this->emdbHelper->deleteTemporaryAssets();
 }
 /**
  * Test submitForm with an fid, ensures values are set correctly.
  *
  * @covers ::ajaxSave
  *
  * @test
  */
 public function ajaxSaveWithFidLoadsEntitiesAndSetsFormStateValues()
 {
     $form = [];
     $catalog_id = 'test_catalog';
     $app_id = 'test_application_id';
     $source_url = 'www.example.com/test_application_id/test.jpg';
     $form['asset']['#catalog_id'] = $catalog_id;
     $form_state = new FormState();
     $intial_values = ['asset' => [self::MOCK_ASSET_ID], 'attributes' => ['data-conversion' => 'thumb']];
     $form_state->setValues($intial_values);
     $mock_asset = $this->getMockBuilder(EmbridgeAssetEntity::class)->disableOriginalConstructor()->getMock();
     $mock_asset->expects($this->once())->method('uuid')->willReturn(self::MOCK_ASSET_UUID);
     $mock_asset->expects($this->once())->method('isTemporary')->willReturn(TRUE);
     $mock_asset->expects($this->once())->method('setPermanent');
     $mock_asset->expects($this->once())->method('save');
     $mock_asset_storage = $this->getMockBuilder(EntityStorageInterface::class)->disableOriginalConstructor()->getMock();
     $mock_asset_storage->expects($this->once())->method('load')->with(self::MOCK_ASSET_ID)->willReturn($mock_asset);
     $mock_catalog = $this->getMockBuilder(EmbridgeCatalog::class)->disableOriginalConstructor()->getMock();
     $mock_catalog->expects($this->once())->method('getApplicationId')->willReturn($app_id);
     $mock_catalog_storage = $this->getMockBuilder(EntityStorageInterface::class)->disableOriginalConstructor()->getMock();
     $mock_catalog_storage->expects($this->once())->method('load')->with($catalog_id)->willReturn($mock_catalog);
     $this->entityTypeManager->expects($this->exactly(2))->method('getStorage')->will($this->returnValueMap([['embridge_asset_entity', $mock_asset_storage], ['embridge_catalog', $mock_catalog_storage]]));
     $this->assetHelper->expects($this->once())->method('getAssetConversionUrl')->with($mock_asset, $app_id, 'thumb')->willReturn($source_url);
     $this->form->ajaxSave($form, $form_state);
     $expected_values = array_merge_recursive(['attributes' => ['src' => $source_url, 'data-entity-uuid' => self::MOCK_ASSET_UUID, 'data-entity-type' => 'embridge_asset_entity']], $intial_values);
     $actual_values = $form_state->getValues();
     $this->assertEquals($expected_values, $actual_values);
 }