diff --git a/config/schema/google_vision.schema.yml b/config/schema/google_vision.schema.yml index 76f0c1a..e85e0b1 100644 --- a/config/schema/google_vision.schema.yml +++ b/config/schema/google_vision.schema.yml @@ -24,3 +24,6 @@ field.field.*.*.*.third_party.google_vision: emotion_detect: type: boolean label: 'Emotion Detection' + features: + type: string + label: 'Fill Alt Text' diff --git a/google_vision.module b/google_vision.module index bc854ec..5e5161d 100644 --- a/google_vision.module +++ b/google_vision.module @@ -196,6 +196,8 @@ function google_vision_file_entity_add_labels($file, $field, $vid) { * Set the value for the alternative text field of the image file. */ function google_vision_set_alt_text($entity, $field) { + // Initialize the output. + $output = ''; $settings = $field->getThirdPartySettings('google_vision'); if (!empty($settings['features'])) { $option = $settings['features']; @@ -251,6 +253,8 @@ function google_vision_set_alt_text($entity, $field) { } break; } - $entity->set('field_image_alt_text', $output); + if (!empty($output)) { + $entity->set('field_image_alt_text', $output); + } } } diff --git a/src/Tests/FillAltTextTest.php b/src/Tests/FillAltTextTest.php new file mode 100644 index 0000000..0aefd50 --- /dev/null +++ b/src/Tests/FillAltTextTest.php @@ -0,0 +1,156 @@ +adminUser = $this->drupalCreateUser([ + 'administer google vision', + 'administer files', + 'edit any image files', + 'create files', + 'administer file types', + 'administer file fields', + ]); + $this->drupalLogin($this->adminUser); + //Check whether the api key is set. + $this->drupalGet(Url::fromRoute('google_vision.settings')); + $this->assertNotNull('api_key', 'The api key is set'); + } + + /** + * Uploads an image file and saves it. + * + * @return integer + * The file id of the newly created image file. + */ + public function uploadImageFile() { + $images = $this->drupalGetTestFiles('image'); + $edit = [ + 'files[upload]' => \Drupal::service('file_system')->realpath($images[0]->uri), + ]; + $this->drupalPostForm('file/add', $edit, t('Next')); + $this->drupalPostForm(NULL, array(), t('Next')); + $this->drupalPostForm(NULL, array(), t('Save')); + return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField(); + } + + /** + * Test to ensure that Label Detection feature fills the Alt Text correctly. + */ + public function testFillAltTextByLabels() { + // Set the Label Detection option to fill the alt text. + $edit = [ + 'features' => 'labels' + ]; + $this->drupalPostForm('admin/structure/file-types/manage/image/edit/fields/file.image.field_image_alt_text', $edit, t('Save settings')); + //Ensure that Label Detection option is set. + $this->drupalGet('admin/structure/file-types/manage/image/edit/fields/file.image.field_image_alt_text'); + // Create an image file. + $file_id = $this->uploadImageFile(); + //Ensure that the Alt Text is filled properly. + $this->drupalGet('file/' . $file_id . '/edit'); + $this->assertFieldByName('field_image_alt_text[0][value]', 'This will be filled with Labels.', 'Alt Text is properly filled with Labels'); + } + + /** + * Test to ensure that Landmark Detection feature fills the Alt Text correctly. + */ + public function testFillAltTextByLandmark() { + // Set the Landmark Detection option to fill the alt text. + $edit = [ + 'features' => 'landmark' + ]; + $this->drupalPostForm('admin/structure/file-types/manage/image/edit/fields/file.image.field_image_alt_text', $edit, t('Save settings')); + //Ensure that Landmark Detection option is set. + $this->drupalGet('admin/structure/file-types/manage/image/edit/fields/file.image.field_image_alt_text'); + // Create an image file. + $file_id = $this->uploadImageFile(); + //Ensure that the Alt Text is filled properly. + $this->drupalGet('file/' . $file_id . '/edit'); + $this->assertFieldByName('field_image_alt_text[0][value]', 'This will be filled with Landmarks.', 'Alt Text is properly set with Landmarks'); + } + + /** + * Test to ensure that Logo Detection feature fills the Alt Text correctly. + */ + public function testFillAltTextByLogo() { + // Set the Logo Detection option to fill the alt text. + $edit = [ + 'features' => 'logo' + ]; + $this->drupalPostForm('admin/structure/file-types/manage/image/edit/fields/file.image.field_image_alt_text', $edit, t('Save settings')); + //Ensure that Logo Detection option is set. + $this->drupalGet('admin/structure/file-types/manage/image/edit/fields/file.image.field_image_alt_text'); + // Create an image file. + $file_id = $this->uploadImageFile(); + //Ensure that the Alt Text is filled properly. + $this->drupalGet('file/' . $file_id . '/edit'); + $this->assertFieldByName('field_image_alt_text[0][value]', 'This will be filled with Logos.', 'Alt Text is properly set with Logos'); + } + + /** + * Test to ensure that Optical Character Detection feature fills the Alt Text correctly. + */ + public function testFillAltTextByOCR() { + // Set the Optical Character Detection option to fill the alt text. + $edit = [ + 'features' => 'ocr' + ]; + $this->drupalPostForm('admin/structure/file-types/manage/image/edit/fields/file.image.field_image_alt_text', $edit, t('Save settings')); + //Ensure that Optical Character Detection option is set. + $this->drupalGet('admin/structure/file-types/manage/image/edit/fields/file.image.field_image_alt_text'); + // Create an image file. + $file_id = $this->uploadImageFile(); + //Ensure that the Alt Text is filled properly. + $this->drupalGet('file/' . $file_id . '/edit'); + $this->assertFieldByName('field_image_alt_text[0][value]', 'This will be filled with Optical Characters.', 'Alt Text is properly set with Optical Characters'); + } + + /** + * Test to ensure that the Alt Text remains empty if no option is selected. + */ + public function testNoOptionSelected() { + // Create an image file. + $file_id = $this->uploadImageFile(); + // Ensure that the Alt Text is empty. + $this->drupalGet('file/' . $file_id . '/edit'); + $this->assertFieldByName('field_image_alt_text[0][value]', '', 'Alt Text is empty'); + } +} diff --git a/tests/modules/google_vision_test/src/GoogleVisionAPIFake.php b/tests/modules/google_vision_test/src/GoogleVisionAPIFake.php index c35f123..a6de7ca 100644 --- a/tests/modules/google_vision_test/src/GoogleVisionAPIFake.php +++ b/tests/modules/google_vision_test/src/GoogleVisionAPIFake.php @@ -49,6 +49,81 @@ class GoogleVisionAPIFake extends GoogleVisionAPI { } /** + * Function to retrieve labels for given image. + * + * @param string $filepath . + * + * @return Array|bool. + */ + public function labelDetection($filepath) { + if (!$this->apiKey) { + return FALSE; + } + $response = [ + 'responses' => [ + '0' => [ + 'labelAnnotations' => [ + '0' => [ + 'description' => 'This will be filled with Labels.', + ], + ], + ], + ], + ]; + return $response; + } + + /** + * Function to detect landmarks within a given image. + * + * @param string $filepath . + * + * @return Array|bool. + */ + public function landmarkDetection($filepath) { + if (!$this->apiKey) { + return FALSE; + } + $response = [ + 'responses' => [ + '0' => [ + 'landmarkAnnotations' => [ + '0' => [ + 'description' => 'This will be filled with Landmarks.', + ], + ], + ], + ], + ]; + return $response; + } + + /** + * Function to detect logos of famous brands within a given image. + * + * @param string $filepath . + * + * @return Array|bool. + */ + public function logoDetection($filepath) { + if (!$this->apiKey) { + return FALSE; + } + $response = [ + 'responses' => [ + '0' => [ + 'logoAnnotations' => [ + '0' => [ + 'description' => 'This will be filled with Logos.', + ], + ], + ], + ], + ]; + return $response; + } + + /** * Function to return the response showing the image contains explicit content. * * @param string $filepath . @@ -73,4 +148,29 @@ class GoogleVisionAPIFake extends GoogleVisionAPI { ); return $response; } + + /** + * Function to retrieve texts for given image. + * + * @param string $filepath . + * + * @return Array|bool. + */ + public function opticalCharacterRecognition($filepath) { + if (!$this->apiKey) { + return FALSE; + } + $response = [ + 'responses' => [ + '0' => [ + 'textAnnotations' => [ + '0' => [ + 'description' => 'This will be filled with Optical Characters.', + ], + ], + ], + ], + ]; + return $response; + } }