Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(REF) Tidy up of CRM_Extension_ManagerTest #25350

Merged
merged 1 commit into from
Jan 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions tests/phpunit/CRM/Extension/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,24 @@ class CRM_Extension_ManagerTest extends CiviUnitTestCase {
const TESTING_TYPE = 'report';
const OTHER_TESTING_TYPE = 'module';

/**
* @var string
*/
protected $basedir;

/**
* @var CRM_Extension_Container_Basic
*/
protected $container;

/**
* @var CRM_Extension_Mapper
*/
protected $mapper;

public function setUp(): void {
parent::setUp();
list ($this->basedir, $this->container) = $this->_createContainer();
list($this->basedir, $this->container) = $this->createContainer();
$this->mapper = new CRM_Extension_Mapper($this->container);
}

Expand Down Expand Up @@ -368,7 +383,7 @@ public function testReplace_Unknown() {
]);
$this->assertEquals('unknown', $manager->getStatus('test.newextension'));

$this->download = $this->_createDownload('test.newextension', 'newextension');
$download = $this->createDownload('test.newextension', 'newextension');

$testingTypeManager
// no data to replace
Expand All @@ -378,7 +393,7 @@ public function testReplace_Unknown() {
// no data to replace
->expects($this->never())
->method('onPostReplace');
$manager->replace($this->download);
$manager->replace($download);
$this->assertEquals('uninstalled', $manager->getStatus('test.newextension'));
$this->assertTrue(file_exists("{$this->basedir}/test.newextension/info.xml"));
$this->assertTrue(file_exists("{$this->basedir}/test.newextension/newextension.php"));
Expand All @@ -397,7 +412,7 @@ public function testReplace_Uninstalled() {
$this->assertEquals('uninstalled', $manager->getStatus('test.whiz.bang'));
$this->assertEquals('oddball', $this->mapper->keyToInfo('test.whiz.bang')->file);

$this->download = $this->_createDownload('test.whiz.bang', 'newextension');
$download = $this->createDownload('test.whiz.bang', 'newextension');

$testingTypeManager
// no data to replace
Expand All @@ -407,7 +422,7 @@ public function testReplace_Uninstalled() {
// no data to replace
->expects($this->never())
->method('onPostReplace');
$manager->replace($this->download);
$manager->replace($download);
$this->assertEquals('uninstalled', $manager->getStatus('test.whiz.bang'));
$this->assertTrue(file_exists("{$this->basedir}/weird/whizbang/info.xml"));
$this->assertTrue(file_exists("{$this->basedir}/weird/whizbang/newextension.php"));
Expand Down Expand Up @@ -435,15 +450,15 @@ public function testReplace_Installed() {
$this->assertEquals('oddball', $this->mapper->keyToInfo('test.whiz.bang')->file);
$this->assertDBQuery('oddball', 'SELECT file FROM civicrm_extension WHERE full_name ="test.whiz.bang"');

$this->download = $this->_createDownload('test.whiz.bang', 'newextension');
$download = $this->createDownload('test.whiz.bang', 'newextension');

$testingTypeManager
->expects($this->once())
->method('onPreReplace');
$testingTypeManager
->expects($this->once())
->method('onPostReplace');
$manager->replace($this->download);
$manager->replace($download);
$this->assertEquals('installed', $manager->getStatus('test.whiz.bang'));
$this->assertTrue(file_exists("{$this->basedir}/weird/whizbang/info.xml"));
$this->assertTrue(file_exists("{$this->basedir}/weird/whizbang/newextension.php"));
Expand Down Expand Up @@ -478,15 +493,15 @@ public function testReplace_InstalledMissing() {
$this->assertEquals('installed-missing', $manager->getStatus('test.whiz.bang'));

// download and reinstall
$this->download = $this->_createDownload('test.whiz.bang', 'newextension');
$download = $this->createDownload('test.whiz.bang', 'newextension');

$testingTypeManager
->expects($this->once())
->method('onPreReplace');
$testingTypeManager
->expects($this->once())
->method('onPostReplace');
$manager->replace($this->download);
$manager->replace($download);
$this->assertEquals('installed', $manager->getStatus('test.whiz.bang'));
$this->assertTrue(file_exists("{$this->basedir}/test.whiz.bang/info.xml"));
$this->assertTrue(file_exists("{$this->basedir}/test.whiz.bang/newextension.php"));
Expand All @@ -500,18 +515,13 @@ public function testReplace_InstalledMissing() {
* @return CRM_Extension_Manager
*/
public function _createManager($typeManagers) {
//list ($basedir, $c) = $this->_createContainer();
$mapper = new CRM_Extension_Mapper($this->container);
return new CRM_Extension_Manager($this->container, $this->container, $this->mapper, $typeManagers);
}

/**
* @param CRM_Utils_Cache_Interface $cache
* @param null $cacheKey
*
* @return array
*/
public function _createContainer(CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL) {
private function createContainer() {
$basedir = $this->createTempDir('ext-');
mkdir("$basedir/weird");
mkdir("$basedir/weird/foobar");
Expand All @@ -523,17 +533,17 @@ public function _createContainer(CRM_Utils_Cache_Interface $cache = NULL, $cache
mkdir("$basedir/weird/downstream");
file_put_contents("$basedir/weird/downstream/info.xml", "<extension key='test.foo.downstream' type='" . self::TESTING_TYPE . "'><file>oddball</file><requires><ext>test.foo.bar</ext></requires></extension>");
// not needed for now // file_put_contents("$basedir/weird/downstream/oddball.php", "<?php\n");
$c = new CRM_Extension_Container_Basic($basedir, 'http://example/basedir', $cache, $cacheKey);
$c = new CRM_Extension_Container_Basic($basedir, 'http://example/basedir', NULL, NULL);
return [$basedir, $c];
}

/**
* @param $key
* @param $file
* @param string $key
* @param string $file
*
* @return string
*/
public function _createDownload($key, $file) {
private function createDownload($key, $file) {
$basedir = $this->createTempDir('ext-dl-');
file_put_contents("$basedir/info.xml", "<extension key='$key' type='" . self::TESTING_TYPE . "'><file>$file</file></extension>");
file_put_contents("$basedir/$file.php", "<?php\n");
Expand Down