-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc44ae1
commit 6e7104a
Showing
6 changed files
with
208 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Drupal\Tests\ckan_connect\Unit\Form; | ||
|
||
use Drupal\Tests\BrowserTestBase; | ||
|
||
/** | ||
* Tests the CkanConnectSettingsForm class. | ||
* | ||
* @group ckan_connect | ||
*/ | ||
class CkanConnectSettingsFormTest extends BrowserTestBase { | ||
|
||
/** | ||
* Modules to enable during test setup. | ||
* | ||
* @var array | ||
*/ | ||
protected static $modules = [ | ||
'ckan_connect', | ||
'user', | ||
]; | ||
|
||
/** | ||
* User with proper permissions for module configuration. | ||
* | ||
* @var \Drupal\user\Entity\User|false | ||
*/ | ||
protected $adminUser; | ||
|
||
/** | ||
* Theme for tests relying on no markup at all or at least no core markup. | ||
* | ||
* @var string | ||
*/ | ||
protected $defaultTheme = 'stark'; | ||
|
||
/** | ||
* The ckan url. | ||
* | ||
* @var string | ||
*/ | ||
protected $ckanUrl = 'https://data.gov.au/data/api/3'; | ||
|
||
/** | ||
* The ckan connect admin config url. | ||
* | ||
* @var string | ||
*/ | ||
protected $adminConfigUrl = '/admin/config/services/ckan-connect'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setUp(): void { | ||
parent::setUp(); | ||
$this->adminUser = $this->drupalCreateUser([ | ||
'administer site configuration', | ||
'administer ckan connect', | ||
]); | ||
|
||
$this->drupalLogin($this->adminUser); | ||
$this->drupalGet($this->adminConfigUrl); | ||
$this->assertSession()->statusCodeEquals(200); | ||
} | ||
|
||
/** | ||
* Test that the correct form fields in the admin config exists. | ||
*/ | ||
public function testCkanConfigForm() { | ||
// Checked that the config fields exist. | ||
$this->assertSession()->pageTextContains("Optionally specify an API key."); | ||
$this->assertSession()->pageTextContains("Specify the endpoint URL. Example https://data.gov.au/api/3 (please note no trailing slash)."); | ||
$this->assertSession()->fieldExists("api[url]"); | ||
$this->assertSession()->fieldExists("api[key]"); | ||
} | ||
|
||
/** | ||
* Test that the config values are saved. | ||
*/ | ||
public function testCkanConfigValuesAreSaved() { | ||
$edit = [ | ||
'api[url]' => $this->ckanUrl, | ||
'api[key]' => '', | ||
]; | ||
|
||
$this->submitForm($edit, 'Save configuration'); | ||
$config_factory = $this->container->get('config.factory'); | ||
$value = $config_factory->get('ckan_connect.settings')->get('api.url'); | ||
$this->assertSame($this->ckanUrl, $value); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Drupal\Tests\ckan_connect\Unit\Form; | ||
|
||
use Drupal\ckan_connect\Parser\CkanResourceUrlParser; | ||
use Drupal\Tests\UnitTestCase; | ||
|
||
/** | ||
* Tests the CkanConnectSettingsForm class. | ||
* | ||
* @group ckan_connect | ||
*/ | ||
class CkanConnectResourceUrlParserTest extends UnitTestCase { | ||
/** | ||
* The Ckan resource parser service. | ||
* | ||
* @var \Drupal\ckan_connect\Parser\CkanResourceUrlParser | ||
*/ | ||
protected $ckanResourceUrlParser; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setUp(): void { | ||
parent::setUp(); | ||
|
||
$ckanClient = $this->createMock('\Drupal\ckan_connect\Client\CkanClientInterface'); | ||
$logger = $this->createMock('\Psr\Log\LoggerInterface'); | ||
$this->ckanResourceUrlParser = new ckanResourceUrlParser($ckanClient, $logger); | ||
} | ||
|
||
/** | ||
* Test that the ckan parser does not recognize invalid urls. | ||
*/ | ||
public function testParserDoesNotRecognizeInvalidUrls() { | ||
$urls = [ | ||
'https://data.gov.au/dataset/ds-dga-d667403f-2016-463f-bb0a-3087ae67c57f/resource-not/0e32d958-3796-4dca-8312-489ef7a610f6', | ||
'https://data.gov.au/data/api/3/action/datastore_search?resource=0e32d958-3796-4dca-8312-489ef7a610f6&limit=5', | ||
'data.gov.au/dataset/ds-dga-d667403f-2016-463f-bb0a-3087ae67c57f', | ||
]; | ||
|
||
foreach ($urls as $url) { | ||
$options = $this->ckanResourceUrlParser->parse($url); | ||
$this->assertFalse($options); | ||
} | ||
} | ||
|
||
/** | ||
* Test that the ckan parser recognizes a correct ckan url. | ||
*/ | ||
public function testParserRecognizesValidCkanUrl() { | ||
$url = 'https://data.gov.au/dataset/ds-dga-d667403f-2016-463f-bb0a-3087ae67c57f/resource/0e32d958-3796-4dca-8312-489ef7a610f6'; | ||
$options = $this->ckanResourceUrlParser->parse($url); | ||
$this->assertIsArray($options); | ||
$this->assertArrayHasKey('resource_id', $options); | ||
$this->assertSame($options['resource_id'], '0e32d958-3796-4dca-8312-489ef7a610f6'); | ||
} | ||
|
||
} |