Commit Message : git commit -m 'Issue #2734841 by ajalan065, naveenvalecha, penyaskito, eugene.ilyin: Implement a runtime requirement checking if API key is not set' --author="ajalan065 <ajalan065@3224995.no-reply.drupal.org>"

At present, the status report does not show up any error if the API key is not set on the admin config page. But setting the key is required to carry out the functionalities of the module.
If the user forgets to set the API key, the status report must reflect the same.

Remaining Tasks:
Develop a patch which would check for the requirement at runtime and would show up the notice on the Status Report page if the key is not set.

Technical Implementation :

Files: 
CommentFileSizeAuthor
#42 2734841-42.patch3.97 KBnaveenvalecha
#32 interdiff-2734841-30-32.txt1004 bytesajalan065
#32 access-modifier-added-8-2734841-8-32.patch3.97 KBajalan065
#30 interdiff-2734841-27-30.txt2.08 KBajalan065
#30 Useless-lines-removed-2734841-8-30.patch3.96 KBajalan065
#27 interdiff-2734841-25-27.txt1.19 KBajalan065
#27 Restriction-on-strictconfigschema-removed-2734841-8-27.patch3.99 KBajalan065
#25 interdiff-2734841-23-25.txt1.42 KBajalan065
#25 test-for-api-key-saved-2734841-8-25.patch3.61 KBajalan065
#23 interdiff-2734841-19-23.txt1.17 KBajalan065
#23 Link-assertion-added-2734841-8-23.patch2.78 KBajalan065
#19 Exception-handling-with-Tests-2734841-8-19.patch2.67 KBajalan065
#18 Test-for-api-key-config-2734841-8-18.patch1.67 KBajalan065
#15 interdiff-2734841-13-15.txt677 bytesajalan065
#15 link-redirected-to-config-page-2734841-8-15.patch1012 bytesajalan065
#13 interdiff-2734841-6-12.txt966 bytesajalan065
#13 title-and-description-changed-2734841-8-12.patch1000 bytesajalan065
#11 Captura de pantalla 2016-05-30 a las 13.40.20.png36.74 KBpenyaskito
#6 interdiff-2734841-4-6.txt1.06 KBajalan065
#6 config-and-fromRoute-used-2734841-8-6.patch987 bytesajalan065
#4 hook-requirements-added-2734841-8-4.patch985 bytesajalan065

Comments

ajalan065 created an issue. See original summary.

ajalan065’s picture

Title: Throw exception if API key is missing » Exceptions and Error Handling
Issue summary: View changes
naveenvalecha’s picture

Issue summary: View changes

I think we then don't need to throw the exception at run time.
Eugene, what do you reckon here ?

ajalan065’s picture

Status: Active » Needs review
FileSize
985 bytes

Here is the patch which implements hook_requirements(), giving the notice at status page if the key is not set.

naveenvalecha’s picture

Status: Needs review » Needs work
  1. +++ b/google_vision.install
    @@ -0,0 +1,26 @@
    +    $key = \Drupal::service('config.factory')->get('google_vision.settings')->get('api_key');
    

    use \Drupal::config('google_vision.settings')->get('api_key');

  2. +++ b/google_vision.install
    @@ -0,0 +1,26 @@
    +        ':settings' => \Drupal::url('google_vision.settings'),
    

    use Url::fromRoute instead of \Drupal::url

ajalan065’s picture

Status: Needs work » Needs review
FileSize
987 bytes
1.06 KB

Changes implemented as suggested in #5.

naveenvalecha’s picture

Issue summary: View changes

let's do it.
create a separate issue for Handling errors in Response.

naveenvalecha’s picture

Status: Needs review » Reviewed & tested by the community
ajalan065’s picture

Title: Exceptions and Error Handling » Exceptions Handling if API key not set

Here is the issue https://www.drupal.org/node/2736567 to handle errors from Google Vision API Response.

penyaskito’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/google_vision.install
@@ -0,0 +1,28 @@
+        'description' => t('The API key is not set at /admin/config/media/google_vision. It is highly required to set the API Key at the <a href=":settings">configuration page</a>.', array(

"The API key is not set at /admin/config/media/google_vision. It is highly required to set the API Key at the :settings page."

I don't see the point of giving an end user a message with a path.
Also, I don't think "highly required" makes sense, it is required or it is not.

Let's change it to something like
"Google Vision API key is not set and it is required for some functionalities to work. Please set it up at the :Google Vision module settings page."

penyaskito’s picture

+++ b/google_vision.install
@@ -0,0 +1,28 @@
+        'title' => t('API Key configuration'),

Also, let's include Google Vision API as the title, so we have the right context. See the following screenshot, and you won't know which API key are we talking about here.

ajalan065’s picture

Status: Needs work » Needs review

New patch and interdiff implementing the changes as suggested in #10 and #11.
Kindly review the patch. :)

ajalan065’s picture

penyaskito’s picture

Status: Needs review » Needs work
+++ b/google_vision.install
@@ -0,0 +1,28 @@
+        'description' => t('Google Vision API key is not set and it is required for some functionalities to work. Please set it up at the <a href=":settings">Google Vision module settings page</a>.', array(

The link is not working.

ajalan065’s picture

Status: Needs work » Needs review
FileSize
1012 bytes
677 bytes

The links are now working.
Please check for any other issues.

penyaskito’s picture

Status: Needs review » Needs work

I think this is a perfect issue for setting up our first WebTest. Please read https://www.drupal.org/contributor-tasks/write-tests for getting started.

ajalan065’s picture

Issue summary: View changes
ajalan065’s picture

Status: Needs work » Needs review
FileSize
1.67 KB

Test for api key configuration

ajalan065’s picture

Here is the patch combining the patch from #15 along with its tests.

penyaskito’s picture

Status: Needs review » Needs work

This looks really great, congrats. Some minor comments:

  1. +++ b/src/Tests/ApiKeyConfigurationTest.php
    @@ -0,0 +1,57 @@
    +  /**
    +   * Modules to install.
    +   *
    +   * @var array
    +   */
    

    Use {@inheritdoc}

  2. +++ b/src/Tests/ApiKeyConfigurationTest.php
    @@ -0,0 +1,57 @@
    +    $config = $this->container->get('config.factory')->get('google_vision.settings');
    

    Is $config used there? I think that statement is useless.

  3. +++ b/src/Tests/ApiKeyConfigurationTest.php
    @@ -0,0 +1,57 @@
    +    if($this->assertFieldByName('api_key', '')) {
    

    Don't use an if. If the field doesn't exist, it will fail the test already.

  4. +++ b/src/Tests/ApiKeyConfigurationTest.php
    @@ -0,0 +1,57 @@
    +      $this->assertText('Google Vision API key is not set and it is required for some functionalities to work.', 'The content exists on the report page');
    

    And assert that the link goes to the right page.

penyaskito’s picture

+++ b/src/Tests/ApiKeyConfigurationTest.php
@@ -0,0 +1,56 @@
+   * Tests that the Google Vision API Key is set in the admin config page.

I forgot: this phpdoc is not accurate, that's not what we are testing.

But we should add a test for testing that too.

penyaskito’s picture

Title: Exceptions Handling if API key not set » Implement a runtime requirement checking if API key is not set
ajalan065’s picture

Status: Needs work » Needs review
FileSize
2.78 KB
1.17 KB

Here is the patch along with the interdiff implementing all the suggestions as in #20.

penyaskito’s picture

Status: Needs review » Needs work

We still need a test for saving the Google Vision API Key and verify that it's stored successfully.

ajalan065’s picture

Status: Needs work » Needs review
FileSize
3.61 KB
1.42 KB

The test for saving the API key and verifying whether it is stored successfully(#24) has been implemented in the attached patch.

naveenvalecha’s picture

Status: Needs review » Needs work
+++ b/src/Tests/ApiKeyConfigurationTest.php
@@ -21,6 +21,15 @@ class ApiKeyConfigurationTest extends WebTestBase {
+   * Disable strict config schema checking.
+   *
+   * The schema is verified at the end of running the update.
+   *
+   * @var bool
+   */
+  protected $strictConfigSchema = FALSE;

Why we are restricting strictconfigschema ?
Define the schema/metadata of the config being used.
See https://www.drupal.org/node/1905070

ajalan065’s picture

Here is the patch implementing the suggestions of #26.

penyaskito’s picture

Status: Needs review » Needs work
  1. +++ b/src/Tests/ApiKeyConfigurationTest.php
    @@ -0,0 +1,73 @@
    +use Drupal\Core\Config\ConfigFactoryInterface;
    

    This use statement is not needed I think? Just looking at the patch, not in context.

  2. +++ b/src/Tests/ApiKeyConfigurationTest.php
    @@ -0,0 +1,73 @@
    +  /**
    +   * Modules to install.
    +   *
    +   * @var array
    +   */
    +  public static $modules = ['google_vision'];
    

    This should be {@inheritdoc}

Otherwise looks good to me.

penyaskito’s picture

  1. +++ b/src/Tests/ApiKeyConfigurationTest.php
    @@ -0,0 +1,73 @@
    +    $this->adminUser = $this->drupalCreateUser(array('administer google vision','administer site configuration')
    +    );
    

    This new line is weird too. And I would prefer short array syntax, but that's a matter of taste I guess.

  2. +++ b/src/Tests/ApiKeyConfigurationTest.php
    @@ -0,0 +1,73 @@
    +
    

    Extra empty line.

  3. +++ b/src/Tests/ApiKeyConfigurationTest.php
    @@ -0,0 +1,73 @@
    +    $apiKey = $this->randomString(40);
    

    Variables are usually using underscore vs camelCase. See https://www.drupal.org/coding-standards#naming

  4. +++ b/src/Tests/ApiKeyConfigurationTest.php
    @@ -0,0 +1,73 @@
    +    $data = array(
    +      'api_key' => $apiKey,
    +    );
    

    Usually we use $edit as a name for this variables, but I don't care that much. Maybe you want to change it for consistence.
    Also, can be a single line.

ajalan065’s picture

Status: Needs work » Needs review
FileSize
3.96 KB
2.08 KB

Here is the new patch implementing the suggestions of #28.
In #29, all the points have been implemented except the first one. Instead of assigning a new array, I merged the two in the same line.
But still I would change it if it is required to.

naveenvalecha’s picture

Status: Needs review » Needs work
+++ b/src/Tests/ApiKeyConfigurationTest.php
@@ -0,0 +1,68 @@
+  function testKeyConfiguration() {

Add the access modifier of the function. same as below one.

ajalan065’s picture

Status: Needs work » Needs review
FileSize
3.97 KB
1004 bytes

Access modifier added as per #31.

penyaskito’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me, thanks :D

ajalan065’s picture

Thanks :)

The last submitted patch, 4: hook-requirements-added-2734841-8-4.patch, failed testing.

The last submitted patch, 6: config-and-fromRoute-used-2734841-8-6.patch, failed testing.

The last submitted patch, 13: title-and-description-changed-2734841-8-12.patch, failed testing.

The last submitted patch, 15: link-redirected-to-config-page-2734841-8-15.patch, failed testing.

The last submitted patch, 18: Test-for-api-key-config-2734841-8-18.patch, failed testing.

eugene.ilyin’s picture

Status: Reviewed & tested by the community » Needs work

Patch looks okay, but in this patch you're modifying the file google_vision.schema.yml. This file is not exists yet. Maybe another issue should be solved befor it?

ajalan065’s picture

Status: Needs work » Needs review

Hi Eugene,
Sorry, I could not understand what you want to convey by

This file is not exists yet.

google_vision.schema.yml is present under the config/schema/google_vision.schema.yml

naveenvalecha’s picture

The patch needs reroll as the permission of the google_vision.schema.yml was changed from 755 to 644 in #2731801: Use of services to access functions
Here's the updated patch with the permission fixing.Hope this will cleanly apply

@ajalan065,
There's nothing major needs to be done here.

let's go for it and enable to drupalci on project :)

naveenvalecha’s picture

Issue summary: View changes

Adding proposed commit mesage

penyaskito’s picture

ajalan065’s picture

@naveenvalecha, Thanks :)

eugene.ilyin’s picture

@ajalan
sorry, I was wrong about the file config/schema/google_vision.schema.yml

naveenvalecha’s picture

Status: Needs review » Reviewed & tested by the community

Thanks! There's was not my code so doing RTBC it.

naveenvalecha’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x-1.x
Well now we have tests :)

naveenvalecha’s picture

Issue summary: View changes

updating commit message.

naveenvalecha’s picture

Issue summary: View changes

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.