-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ Adds Orcid data import optimization feature tests
Includes ProfileDataFactory for publications and authors, publications mock and feature test.
- Loading branch information
1 parent
cacd441
commit 628400d
Showing
5 changed files
with
293 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Profile; | ||
use App\ProfileData; | ||
use Tests\TestCase; | ||
use App\Helpers\Publication; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Tests\Feature\Traits\LoginWithRole; | ||
use Illuminate\Foundation\Testing\WithFaker; | ||
use Tests\Feature\Traits\MockPublicationsRepository; | ||
use App\Repositories\OrcidPublicationsRepository; | ||
use Tests\Feature\Traits\HasJson; | ||
|
||
class PublicationsRepositoryTest extends TestCase | ||
{ | ||
use MockPublicationsRepository; | ||
use RefreshDatabase; | ||
use WithFaker; | ||
use LoginWithRole; | ||
use HasJson; | ||
|
||
/** | ||
* Indicates whether the default seeder should run before each test. | ||
* | ||
* @var bool | ||
*/ | ||
protected $seed = true; | ||
public array $existing_publications_data; | ||
public Profile $profile; | ||
|
||
/** @test | ||
** @group orcid_import | ||
**/ | ||
public function testImportOrcidPublications() | ||
{ | ||
$this->profile = Profile::factory() | ||
->hasData([ | ||
'data->orc_id_managed' => 1, | ||
'data->orc_id' => $this->faker()->numerify(), | ||
]) | ||
->has( | ||
ProfileData::factory() //count = 3 | ||
->existing_publication('general'), | ||
'data') | ||
->has( | ||
ProfileData::factory() | ||
->count(2) | ||
->state([ | ||
'type' => 'publications', | ||
'data->sort_order' => $this->faker->year() | ||
]) | ||
->general(), 'data') | ||
->create(); | ||
|
||
$this->assertTrue($this->profile->hasOrcidManagedPublications()); | ||
|
||
$this->assertCount(5, $this->profile->publications); | ||
$this->assertDatabaseCount('profile_data', 6); | ||
|
||
// $this->output("PROFILE PUBLICATIONS CREATED", $this->profile->publications, ['profile_id', 'sort_order', 'title']); | ||
|
||
$publications_edit_route = route('profiles.edit', [ | ||
'profile' => $this->profile, | ||
'section' => 'publications', | ||
]); | ||
|
||
$orcid_pubs_repo = $this->mockPublicationsRepository(); | ||
|
||
$this->instance(OrcidPublicationsRepository::class, $orcid_pubs_repo); | ||
$this->loginAsAdmin(); | ||
|
||
$this->followingRedirects() | ||
->get($publications_edit_route) | ||
->assertStatus(200) | ||
->assertViewIs('profiles.show') | ||
->assertSee('Publications updated via ORCID.'); | ||
|
||
$this->profile->refresh(); | ||
$this->assertCount(9, $this->profile->publications); | ||
$this->assertDatabaseCount('profile_data', 10); | ||
|
||
foreach ($this->profile->publications as $orcid_pub) { | ||
$this->assertDatabaseHas( | ||
'profile_data', | ||
['data' => $this->castToJson((array)$orcid_pub->data)] | ||
); | ||
|
||
if (isset($orcid_pub->data['authors'])) { | ||
|
||
$authors = Publication::formatAuthorsNames($orcid_pub->data['authors']); | ||
|
||
foreach ($authors as $author) { | ||
$this->assertMatchesRegularExpression(Publication::REGEX_PATTERNS['APA'], $author['APA']); | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Output a message to the console and log file | ||
*/ | ||
public function output(string $message, $items = null, $attributes = null ): void | ||
{ | ||
echo "\n $message \n"; | ||
|
||
if (!is_null($items)) { | ||
|
||
foreach ($items as $key => $item) { | ||
$string = "$key "; | ||
foreach ($attributes as $attr) { | ||
$string .= $item->$attr . " "; | ||
} | ||
$string .= "\n"; | ||
echo $string; | ||
} | ||
} | ||
} | ||
} |
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,65 @@ | ||
<?php | ||
|
||
namespace Tests\Feature\Traits; | ||
|
||
use App\ProfileData; | ||
use App\Repositories\OrcidPublicationsRepository; | ||
|
||
trait MockPublicationsRepository | ||
{ | ||
/** | ||
* Partial mock to return the orcid API response containing publications (ProfileData collection) | ||
* | ||
* @return OrcidPublicationsRepository | ||
*/ | ||
public function mockPublicationsRepository() | ||
{ | ||
$publications = $this->makePublications(); | ||
|
||
// $this->output("API PUBLICATIONS TO SYNC", $publications, ['profile_id', 'sort_order', 'title']); | ||
|
||
$pubs_mock = mock(OrcidPublicationsRepository::class)->makePartial(); | ||
|
||
$pubs_mock | ||
->shouldReceive('getCachedPublications') | ||
->andReturn($publications); | ||
|
||
return $pubs_mock; | ||
} | ||
|
||
/** | ||
* Returns a ProfileDataFactory collection of publications exisisting in the DB and new publications | ||
* | ||
* @return \Illuminate\Support\Collection<ProfileDataFactory> | ||
*/ | ||
public function makePublications() | ||
{ | ||
$orcid_api_new_pubs = | ||
ProfileData::factory() | ||
->count(4) | ||
->orcid_publication($this->profile) | ||
->make(); | ||
|
||
$orcid_api_existing_pubs = | ||
ProfileData::factory() //count = 3 | ||
->existing_publication('orcid_publication', $this->profile) | ||
->make(); | ||
|
||
$orcid_api_new_pubs->map(fn($pub) => $orcid_api_existing_pubs->push($pub)); | ||
|
||
return $orcid_api_existing_pubs; | ||
} | ||
|
||
/** | ||
* Clean up the testing environment before the next test. | ||
* | ||
* @return void | ||
*/ | ||
protected function tearDown(): void | ||
{ | ||
// fix for the config() helper not resolving in tests using Mockery | ||
$config = app('config'); | ||
parent::tearDown(); | ||
app()->instance('config', $config); | ||
} | ||
} |