Skip to content

Commit 304a8d8

Browse files
committedMay 28, 2024·
Added unit tests
1 parent b263111 commit 304a8d8

File tree

4 files changed

+160
-61
lines changed

4 files changed

+160
-61
lines changed
 

‎modules/stanford_person/modules/stanford_person_importer/src/Config/ConfigOverrides.php

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public function loadOverrides($names) {
6161
$overrides['migrate_plus.migration.su_stanford_person']['source']['plugin'] = 'cap_url';
6262
$overrides['migrate_plus.migration.su_stanford_person']['source']['authentication']['client_id'] = $this->getCapClientId();
6363
$overrides['migrate_plus.migration.su_stanford_person']['source']['authentication']['client_secret'] = $this->getCapClientSecret();
64+
65+
$overrides['migrate_plus.migration.su_stanford_person']['status'] = $this->getCapClientId() && $this->getCapClientSecret();
6466
}
6567
return $overrides;
6668
}

‎modules/stanford_person/modules/stanford_person_importer/src/Plugin/migrate/source/CapUrl.php

+36-27
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
namespace Drupal\stanford_person_importer\Plugin\migrate\source;
44

5+
use Drupal\config_pages\ConfigPagesLoaderServiceInterface;
6+
use Drupal\Core\Config\ConfigFactoryInterface;
7+
use Drupal\Core\Entity\EntityTypeManagerInterface;
8+
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
59
use Drupal\Core\Url;
610
use Drupal\migrate\Plugin\MigrationInterface;
711
use Drupal\migrate_plus\Plugin\migrate\source\Url as UrlPlugin;
12+
use Drupal\stanford_person_importer\CapInterface;
13+
use Symfony\Component\DependencyInjection\ContainerInterface;
814

915
/**
1016
* Source plugin for retrieving data via CAP URLs.
@@ -13,38 +19,42 @@
1319
* id = "cap_url"
1420
* )
1521
*/
16-
class CapUrl extends UrlPlugin {
22+
class CapUrl extends UrlPlugin implements ContainerFactoryPluginInterface {
1723

1824
const URL_CHUNKS = 15;
1925

2026
/**
21-
* Cap service.
22-
*
23-
* @var \Drupal\stanford_person_importer\CapInterface
27+
* {@inheritDoc}
2428
*/
25-
protected $cap;
26-
27-
protected $configPages;
28-
29-
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) {
30-
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
31-
32-
$this->cap = \Drupal::service('stanford_person_importer.cap');
33-
$this->configPages = \Drupal::service('@config_pages.loader');
34-
$this->configFactory = \Drupal::configFactory();
35-
$this->entityTypeManager = \Drupal::entityTypeManager();
29+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
30+
return new static(
31+
$configuration,
32+
$plugin_id,
33+
$plugin_definition,
34+
$migration,
35+
$container->get('stanford_person_importer.cap'),
36+
$container->get('config_pages.loader'),
37+
$container->get('config.factory'),
38+
$container->get('entity_type.manager')
39+
);
40+
}
3641

42+
/**
43+
* {@inheritDoc}
44+
*/
45+
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, protected CapInterface $cap, protected ConfigPagesLoaderServiceInterface $configPages, protected ConfigFactoryInterface $configFactory, protected EntityTypeManagerInterface $entityTypeManager) {
3746
$this->sourceUrls = $this->getImporterUrls();
47+
$configuration['urls'] = $this->sourceUrls;
48+
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
3849
}
3950

40-
4151
/**
4252
* Get a list of urls for the importer.
4353
*
4454
* @return array|null
4555
* Array of urls or NULL if any errors occur.
4656
*/
47-
protected function getImporterUrls(): ?array {
57+
protected function getImporterUrls(): array {
4858
$urls = &drupal_static('cap_source_urls');
4959
if ($urls !== NULL) {
5060
return $urls;
@@ -58,7 +68,7 @@ protected function getImporterUrls(): ?array {
5868
$urls = array_merge($urls, $this->getSunetUrls());
5969
}
6070
catch (\Exception $e) {
61-
return NULL;
71+
return [];
6272
}
6373

6474
$allowed_fields = $this->getAllowedFields();
@@ -77,8 +87,8 @@ protected function getImporterUrls(): ?array {
7787
* Array of CAP selectors.
7888
*/
7989
protected function getAllowedFields() {
80-
$allowed_fields = $this->configFactory->getEditable('migrate_plus.migration.su_stanford_person')
81-
->getOriginal('source.fields') ?: [];
90+
$allowed_fields = $this->configFactory->get('migrate_plus.migration.su_stanford_person')
91+
->get('source.fields') ?: [];
8292
foreach ($allowed_fields as &$field) {
8393
$field = $field['selector'];
8494
if ($slash_position = strpos($field, '/')) {
@@ -128,11 +138,7 @@ protected function getOrgsUrls() {
128138
*/
129139
protected function getWorkgroupUrls(): array {
130140
$workgroups = array_filter($this->configPages->getValue('stanford_person_importer', 'su_person_workgroup', [], 'value') ?? []);
131-
132-
if ($workgroups) {
133-
return $this->getUrlChunks($this->cap->getWorkgroupUrl($workgroups));
134-
}
135-
return [];
141+
return $workgroups ? $this->getUrlChunks($this->cap->getWorkgroupUrl($workgroups)) : [];
136142
}
137143

138144
/**
@@ -146,7 +152,9 @@ protected function getSunetUrls(): array {
146152

147153
$urls = [];
148154
foreach (array_chunk($sunets, self::URL_CHUNKS) as $chunk) {
149-
$urls[] = $this->cap->getSunetUrl($chunk)->toString(TRUE)->getGeneratedUrl();
155+
$urls[] = $this->cap->getSunetUrl($chunk)
156+
->toString(TRUE)
157+
->getGeneratedUrl();
150158
}
151159
return $urls;
152160
}
@@ -195,4 +203,5 @@ protected function getCapClientId(): string {
195203
protected function getCapClientSecret(): string {
196204
return $this->configPages->getValue('stanford_person_importer', 'su_person_cap_password', 0, 'value') ?? '';
197205
}
198-
}
206+
207+
}

‎modules/stanford_person/modules/stanford_person_importer/tests/src/Unit/Config/ConfigOverridesTest.php

+3-34
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ protected function getCapService() {
106106
->willReturn(Url::fromUri('http://localhost.orgs'));
107107
$cap->method('getWorkgroupUrl')
108108
->willReturn(Url::fromUri('http://localhost.workgroup'));
109-
$cap->method('getSunetUrl')
110-
->will($this->returnCallback([$this, 'getSunetUrlCallback']));
111109

112110
return $cap;
113111
}
@@ -127,13 +125,12 @@ public function testBasicMethods() {
127125
}
128126

129127
/**
130-
* Test the config overrides when theres no urls.
128+
* Test the config overrides when there's no urls.
131129
*/
132130
public function testEmptyConfigOverrides() {
133131
$overrides = $this->configOverrides->loadOverrides(['migrate_plus.migration.su_stanford_person']);
134132
$this->assertEmpty($overrides['migrate_plus.migration.su_stanford_person']['source']['authentication']['client_id']);
135133
$this->assertEmpty($overrides['migrate_plus.migration.su_stanford_person']['source']['authentication']['client_secret']);
136-
$this->assertEmpty($overrides['migrate_plus.migration.su_stanford_person']['source']['urls']);
137134
}
138135

139136
/**
@@ -156,38 +153,10 @@ public function testConfigOverrides() {
156153
}
157154
});
158155

159-
$this->configOverrides->loadOverrides(['migrate_plus.migration.su_stanford_person']);
160-
161-
drupal_static_reset('cap_source_urls');
162-
$overrides = $this->configOverrides->loadOverrides(['migrate_plus.migration.su_stanford_person']);
163-
164-
$expected_urls = [
165-
'http://localhost.orgs?ps=15&whitelist=fooBar,barFoo',
166-
'http://localhost.workgroup?p=1&ps=15&whitelist=fooBar,barFoo',
167-
'http://localhost.workgroup?p=2&ps=15&whitelist=fooBar,barFoo',
168-
'http://localhost.sunet?whitelist=fooBar,barFoo',
169-
];
170-
asort($expected_urls);
171-
asort($overrides['migrate_plus.migration.su_stanford_person']['source']['urls']);
172-
foreach ($overrides['migrate_plus.migration.su_stanford_person']['source']['urls'] as &$url) {
173-
$url = urldecode($url);
174-
}
175-
$this->assertEquals(array_values($expected_urls), array_values($overrides['migrate_plus.migration.su_stanford_person']['source']['urls']));
176-
177-
$this->sunetUrlError = TRUE;
178-
drupal_static_reset('cap_source_urls');
179156
$overrides = $this->configOverrides->loadOverrides(['migrate_plus.migration.su_stanford_person']);
180-
$this->assertFalse($overrides['migrate_plus.migration.su_stanford_person']['status']);
181-
}
182157

183-
/**
184-
* Cap mock service callback.
185-
*/
186-
public function getSunetUrlCallback() {
187-
if ($this->sunetUrlError) {
188-
throw new \Exception('Error getting sunet url');
189-
}
190-
return Url::fromUri('http://localhost.sunet');
158+
$this->assertEquals('foo', $overrides['migrate_plus.migration.su_stanford_person']['source']['authentication']['client_id']);
159+
$this->assertEquals('bar', $overrides['migrate_plus.migration.su_stanford_person']['source']['authentication']['client_secret']);
191160
}
192161

193162
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
namespace Drupal\Tests\stanford_person_importer\Unit\Plugin\migrate\source;
4+
5+
use Drupal\config_pages\ConfigPagesLoaderServiceInterface;
6+
use Drupal\Core\DependencyInjection\ContainerBuilder;
7+
use Drupal\Core\Entity\ContentEntityInterface;
8+
use Drupal\Core\Entity\EntityStorageInterface;
9+
use Drupal\Core\Entity\EntityTypeManagerInterface;
10+
use Drupal\Core\Field\FieldItemListInterface;
11+
use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
12+
use Drupal\Core\Url;
13+
use Drupal\Core\Utility\UnroutedUrlAssembler;
14+
use Drupal\migrate\Plugin\MigrationInterface;
15+
use Drupal\stanford_person_importer\CapInterface;
16+
use Drupal\stanford_person_importer\Plugin\migrate\source\CapUrl;
17+
use Drupal\Tests\UnitTestCase;
18+
use Symfony\Component\HttpFoundation\Request;
19+
use Symfony\Component\HttpFoundation\RequestStack;
20+
21+
class CapUrlTest extends UnitTestCase {
22+
23+
protected $plugin;
24+
25+
protected $profileCount = 10;
26+
27+
public function testUrls() {
28+
$config_pages = $this->createMock(ConfigPagesLoaderServiceInterface::class);
29+
30+
$config_pages->method('getValue')
31+
->will($this->returnCallback([$this, 'getConfigPageValue']));
32+
$entity = $this->createMock(ContentEntityInterface::class);
33+
$field_list = $this->createMock(FieldItemListInterface::class);
34+
$field_list->method('getString')->willReturn('org:code');
35+
$entity->method('get')->willReturn($field_list);
36+
$entity_storage = $this->createMock(EntityStorageInterface::class);
37+
$entity_storage->method('load')->willReturn($entity);
38+
$entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
39+
$entity_type_manager->method('getStorage')->willReturn($entity_storage);
40+
$migration = $this->createMock(MigrationInterface::class);
41+
42+
$container = new ContainerBuilder();
43+
44+
$container->set('config_pages.loader', $config_pages);
45+
$container->set('config.factory', $this->getConfigFactoryStub([
46+
'migrate_plus.migration.su_stanford_person' => [
47+
'source' => [
48+
'fields' => [
49+
['selector' => 'foo'],
50+
['selector' => 'bar/bin/foo'],
51+
['selector' => 'baz'],
52+
],
53+
],
54+
],
55+
]));
56+
$container->set('entity_type.manager', $entity_type_manager);
57+
$container->set('unrouted_url_assembler', $this->getUrlAssembler());
58+
59+
$cap = $this->createMock(CapInterface::class);
60+
$cap->method('getTotalProfileCount')
61+
->will($this->returnCallback([$this, 'getCapProfileCount']));
62+
$cap->method('getOrganizationUrl')
63+
->willReturn(Url::fromUri('http://orgurl'));
64+
$cap->method('getWorkgroupUrl')
65+
->willReturn(Url::fromUri('http://workgroupurl'));
66+
$cap->method('getSunetUrl')
67+
->willReturn(Url::fromUri('http://suneturl'));
68+
69+
$container->set('stanford_person_importer.cap', $cap);
70+
\Drupal::setContainer($container);
71+
72+
$plugin = TestCapUrl::create($container, [
73+
'fields' => [],
74+
'ids' => [],
75+
], 'cap_url', [], $migration);
76+
77+
$this->assertEquals([
78+
'http://orgurl?ps=15&whitelist=foo%2Cbar%2Cbaz',
79+
'http://workgroupurl?p=1&ps=15&whitelist=foo%2Cbar%2Cbaz',
80+
'http://workgroupurl?p=2&ps=15&whitelist=foo%2Cbar%2Cbaz',
81+
'http://suneturl?whitelist=foo%2Cbar%2Cbaz',
82+
], $plugin->getSourceUrls());
83+
}
84+
85+
public function getConfigPageValue($bundle, $field, $delta, $key) {
86+
switch ($field) {
87+
case 'su_person_orgs':
88+
return [1, 2, 3];
89+
case 'su_person_child_orgs':
90+
return FALSE;
91+
case 'su_person_workgroup':
92+
return ['bar:foo', 'bin:foo'];
93+
case 'su_person_sunetid':
94+
return ['foofoofoo'];
95+
}
96+
}
97+
98+
protected function getUrlAssembler() {
99+
$request_stack = new RequestStack();
100+
$request_stack->push(new Request());
101+
$path_processor = $this->createMock(OutboundPathProcessorInterface::class);
102+
return new UnroutedUrlAssembler($request_stack, $path_processor);
103+
}
104+
105+
public function getCapProfileCount() {
106+
$count = $this->profileCount;
107+
$this->profileCount += 10;
108+
return $count;
109+
}
110+
111+
}
112+
113+
class TestCapUrl extends CapUrl {
114+
115+
public function getSourceUrls() {
116+
return $this->sourceUrls;
117+
}
118+
119+
}

0 commit comments

Comments
 (0)
Please sign in to comment.