diff --git a/src/Tests/SafeSearchConstraintValidationTest.php b/src/Tests/SafeSearchConstraintValidationTest.php index 764ec71..a87d83f 100644 --- a/src/Tests/SafeSearchConstraintValidationTest.php +++ b/src/Tests/SafeSearchConstraintValidationTest.php @@ -14,6 +14,8 @@ use Drupal\Core\Field\FieldItemListInterface; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Drupal\google_vision\GoogleVisionAPI; +use Drupal\file\Entity\File; +use Drupal\file\FileInterface; /** * Tests to verify whether the Safe Search Constraint Validation works @@ -47,13 +49,9 @@ class SafeSearchConstraintValidationTest extends WebTestBase { $this->adminUser = $this->drupalCreateUser(array('administer google vision','create test_images content', 'access content', 'access administration pages', 'administer node fields', 'administer nodes', 'administer node display') ); $this->drupalLogin($this->adminUser); - - //Set the API Key. - /*\Drupal::configFactory()->getEditable('google_vision.settings')->set('api_key', 'AIzaSyAtuc_LOB70imSYsT0TXFUNnNlMqiDoyP8')->save(); + //Check whether the api key is set. $this->drupalGet(Url::fromRoute('google_vision.settings')); - $this->assertResponse(200); - $this->assertFieldByName('api_key', 'AIzaSyAtuc_LOB70imSYsT0TXFUNnNlMqiDoyP8', 'The key has been saved'); - */ + $this->assertNotNull('api_key', 'The api key is set'); } /** @@ -139,15 +137,15 @@ class SafeSearchConstraintValidationTest extends WebTestBase { } /** - * Upload an image to the node of type test_images and create the node. - * - * @param The image file $image - * A file object representing the image to upload. + * Create a node of type test_images and also upload an image. */ - function uploadImageFile($image) { + public function createNodeWithImage() { + //Get an image. + $images = $this->drupalGetTestFiles('image'); + $edit = array( 'title[0][value]' => $this->randomMachineName(), - 'files[images_0]' => drupal_realpath($image->getFileUri()), + 'files[images_0]' => drupal_realpath($images[0]->uri), ); $this->drupalPostForm('node/add/test_images' , $edit, t('Save and publish')); @@ -177,12 +175,9 @@ class SafeSearchConstraintValidationTest extends WebTestBase { //Ensure that the safe search is enabled. $this->drupalGet("admin/structure/types/manage/test_images/fields/$field_id"); - // Get an image with explicit content from web. - $image = file_get_contents('http://www.menshealth.com/sites/menshealth.com/files/articles/2015/12/man-snoring.jpg'); // string - $file = file_save_data($image, 'public://explicit.jpg', FILE_EXISTS_REPLACE); - // Save the node. - $node_id = $this->uploadImageFile($file); + $node_id = $this->createNodeWithImage(); + //Assert the constraint message. $this->assertText('This image contains explicit content and will not be saved.', 'Constraint message found'); //Assert that the node is not saved. @@ -199,14 +194,10 @@ class SafeSearchConstraintValidationTest extends WebTestBase { //Ensure that the safe search is disabled. $this->drupalGet("admin/structure/types/manage/test_images/fields/$field_id"); - // Get an image with explicit content from web. - - $image = file_get_contents('http://www.menshealth.com/sites/menshealth.com/files/articles/2015/12/man-snoring.jpg'); // string - $file = file_save_data($image, 'public://explicit.jpg', FILE_EXISTS_REPLACE); - // Save the node. - $node_id = $this->uploadImageFile($file); - //Assert the constraint message. + $node_id = $this->createNodeWithImage(); + + //Assert that no constraint message appears. $this->assertNoText('This image contains explicit content and will not be saved.', 'No Constraint message found'); //Display the node. $this->drupalGet('node/' . $node_id); diff --git a/tests/modules/google_vision_test/google_vision_test.info.yml b/tests/modules/google_vision_test/google_vision_test.info.yml index fcaa773..1db3d18 100644 --- a/tests/modules/google_vision_test/google_vision_test.info.yml +++ b/tests/modules/google_vision_test/google_vision_test.info.yml @@ -1,8 +1,7 @@ -name: 'Google Vision Test Helper' +name: Google Vision Test Helper type: module description: 'Mocks the Google Vision Service' package: Testing -# version: VERSION -# core: 8.x +core: 8.x dependencies: - google_vision diff --git a/tests/modules/google_vision_test/google_vision_test.install b/tests/modules/google_vision_test/google_vision_test.install new file mode 100644 index 0000000..943cf03 --- /dev/null +++ b/tests/modules/google_vision_test/google_vision_test.install @@ -0,0 +1,10 @@ +getEditable('google_vision.settings'); + $config->set('api_key', 'test_23key') + ->save(); +} diff --git a/tests/modules/google_vision_test/src/GoogleVisionAPIFake.php b/tests/modules/google_vision_test/src/GoogleVisionAPIFake.php index e660ffe..c35f123 100644 --- a/tests/modules/google_vision_test/src/GoogleVisionAPIFake.php +++ b/tests/modules/google_vision_test/src/GoogleVisionAPIFake.php @@ -33,32 +33,40 @@ class GoogleVisionAPIFake extends GoogleVisionAPI { /** - * Construct a GoogleVisionAPI object. + * Construct a GoogleVisionAPIFake object. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory. * * @param \GuzzleHttp\ClientInterface $http_client * A Guzzle client object. - * - * @todo Throw the exception when the api key is not set on status page. */ public function __construct(ConfigFactoryInterface $config_factory, ClientInterface $http_client) { $this->configFactory = $config_factory; $this->httpClient = $http_client; - /*$this->apiKey = $this->configFactory->get('google_vision.settings') - ->get('api_key');*/ + $this->apiKey = $this->configFactory->get('google_vision.settings') + ->get('api_key'); } + /** + * Function to return the response showing the image contains explicit content. + * + * @param string $filepath . + * + * @return Array|bool. + */ public function safeSearchDetection($filepath) { - /*if(!$this->apiKey) { + if (!$this->apiKey) { return FALSE; - }*/ + } $response = array( 'responses' => array( '0' => array( 'safeSearchAnnotation' => array( - 'adult' => 'LIKELY' + 'adult' => 'LIKELY', + 'spoof' => 'VERY_UNLIKELY', + 'medical' => 'POSSIBLE', + 'violence' => 'POSSIBLE' ), ), ), diff --git a/tests/modules/google_vision_test/src/GoogleVisionTestServiceProvider.php b/tests/modules/google_vision_test/src/GoogleVisionTestServiceProvider.php index 0c9f54d..ef9d68f 100644 --- a/tests/modules/google_vision_test/src/GoogleVisionTestServiceProvider.php +++ b/tests/modules/google_vision_test/src/GoogleVisionTestServiceProvider.php @@ -11,7 +11,8 @@ class GoogleVisionTestServiceProvider extends ServiceProviderBase { * {@inheritdoc} */ public function alter(ContainerBuilder $container) { - // Overrides lingotek class to mock communication with the server. + // Overrides GoogleVisionAPI class to mock the safe search feature. $definition = $container->getDefinition('google_vision.api'); $definition->setClass('Drupal\google_vision_test\GoogleVisionAPIFake'); + } }