Skip to content

Commit

Permalink
Merge pull request #23 from iMattPro/testing
Browse files Browse the repository at this point in the history
Test updates
  • Loading branch information
iMattPro authored Jan 6, 2025
2 parents e4e4199 + b032a5f commit 76849b9
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 40 deletions.
141 changes: 104 additions & 37 deletions tests/functional/acp_file_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
*/
class acp_file_test extends phpbb_functional_test_case
{
private string $path;
private string $fixtures;
private string $icons;

protected static function setup_extensions(): array
{
Expand All @@ -35,27 +36,29 @@ protected function setUp(): void

parent::setUp();

$this->path = __DIR__ . '/../fixtures/';
$this->fixtures = __DIR__ . '/../fixtures/';
$this->icons = __DIR__ . '/../../../../../images/site_icons/';

$this->add_lang('posting');
$this->add_lang_ext('phpbb/pwakit', ['acp_pwa', 'info_acp_pwa']);
}

protected function tearDown(): void
{
$iterator = new DirectoryIterator(__DIR__ . '/../../../../../images/site_icons/');
foreach ($iterator as $fileinfo)
$iterator = new DirectoryIterator($this->icons);
foreach ($iterator as $fileInfo)
{
if (
$fileinfo->isDot()
|| $fileinfo->isDir()
|| $fileinfo->getFilename() === 'index.htm'
|| $fileinfo->getFilename() === '.htaccess'
$fileInfo->isDot()
|| $fileInfo->isDir()
|| $fileInfo->getFilename() === 'index.htm'
|| $fileInfo->getFilename() === '.htaccess'
)
{
continue;
}

unlink($fileinfo->getPathname());
unlink($fileInfo->getPathname());
}
}

Expand All @@ -73,10 +76,10 @@ private function upload_file($filename, $mimetype): Crawler
$file_form_data = array_merge(['upload' => $this->lang('ACP_PWA_IMG_UPLOAD_BTN')], $this->get_hidden_fields($crawler, $url));

$file = [
'tmp_name' => $this->path . $filename,
'tmp_name' => $this->fixtures . $filename,
'name' => $filename,
'type' => $mimetype,
'size' => filesize($this->path . $filename),
'size' => filesize($this->fixtures . $filename),
'error' => UPLOAD_ERR_OK,
];

Expand Down Expand Up @@ -110,64 +113,128 @@ public function test_upload_invalid_extension()

public function test_upload_valid_file()
{
$test_image = 'foo.png';

// Check icon does not yet appear in the html tags
$crawler = self::request('GET', 'index.php');
$this->assertCount(0, $crawler->filter('link[rel="apple-touch-icon"]'));
$this->assertAppleTouchIconNotPresent();

$this->login();
$this->admin_login();

$crawler = $this->upload_file('foo.png', 'image/png');
$crawler = $this->upload_file($test_image, 'image/png');

// Ensure there was no error message rendered
$this->assertContainsLang('ACP_PWA_IMG_UPLOAD_SUCCESS', $crawler->text());

// Check icon appears in the ACP as expected
$crawler = self::request('GET', 'adm/index.php?i=-phpbb-pwakit-acp-pwa_acp_module&mode=settings&sid=' . $this->sid);
$this->assertStringContainsString('foo.png', $crawler->filter('fieldset')->eq(2)->text());
$this->assertIconInACP($test_image);

// Check icon appears in the html tags as expected
$crawler = self::request('GET', 'index.php?sid=' . $this->sid);
$this->assertStringContainsString('foo.png', $crawler->filter('link[rel="apple-touch-icon"]')->attr('href'));
$this->assertAppleTouchIconPresent($test_image);
}

public function test_resync_delete_file()
{
$test_image = 'bar.png';

// Manually copy image to site icon dir
copy($this->path . 'bar.png', __DIR__ . '/../../../../../images/site_icons/bar.png');
@copy($this->fixtures . $test_image, $this->icons . $test_image);

// Check icon does not appear in the html tags
$crawler = self::request('GET', 'index.php');
$this->assertCount(0, $crawler->filter('link[rel="apple-touch-icon"]'));
$this->assertAppleTouchIconNotPresent();

$this->login();
$this->admin_login();

// Ensure copied image does not appear
$crawler = self::request('GET', 'adm/index.php?i=-phpbb-pwakit-acp-pwa_acp_module&mode=settings&sid=' . $this->sid);
$this->assertContainsLang('ACP_PWA_KIT_NO_ICONS', $crawler->filter('fieldset')->eq(2)->html());
// Ensure copied image does not appear in ACP
$crawler = $this->assertIconsNotInACP();

// Resync image
// Resync image and then verify icon appears in the html tags as expected
$this->performResync($crawler, $test_image);
$this->assertAppleTouchIconPresent($test_image);

// Delete image
$this->performDelete($test_image);
$this->assertIconsNotInACP();

// Check icon does not appear in the html tags
$this->assertAppleTouchIconNotPresent();
}

/**
* Perform the resync action
*
* @param Crawler $crawler
* @param string $expected The name of an icon/image expected to see after resync
* @return void
*/
private function performResync(Crawler $crawler, string $expected): void
{
$form = $crawler->selectButton('resync')->form();
$crawler = self::submit($form);
$this->assertStringContainsString('bar.png', $crawler->filter('fieldset')->eq(2)->text());

// Check icon appears in the html tags as expected
$crawler = self::request('GET', 'index.php?sid=' . $this->sid);
$this->assertStringContainsString('bar.png', $crawler->filter('link[rel="apple-touch-icon"]')->attr('href'));
$this->assertStringContainsString($expected, $crawler->filter('fieldset')->eq(2)->text());
}

// Delete image
/**
* Perform the delete action for an icon
*
* @param string $icon
* @return void
*/
private function performDelete(string $icon): void
{
$crawler = self::request('GET', 'adm/index.php?i=-phpbb-pwakit-acp-pwa_acp_module&mode=settings&sid=' . $this->sid);
$form = $crawler->selectButton('delete')->form(['delete' => 'bar.png']);
$form = $crawler->selectButton('delete')->form(['delete' => $icon]);
$crawler = self::submit($form);
$form = $crawler->selectButton('confirm')->form(['delete' => 'bar.png']);
$form = $crawler->selectButton('confirm')->form(['delete' => $icon]);
$crawler = self::submit($form);
$this->assertStringContainsString($this->lang('ACP_PWA_IMG_DELETED', 'bar.png'), $crawler->text());
$crawler = self::request('GET', 'adm/index.php?i=-phpbb-pwakit-acp-pwa_acp_module&mode=settings&sid=' . $this->sid);
$this->assertContainsLang('ACP_PWA_KIT_NO_ICONS', $crawler->filter('fieldset')->eq(2)->html());
$this->assertStringContainsString($this->lang('ACP_PWA_IMG_DELETED', $icon), $crawler->text());
}

// Check icon does not appear in the html tags
/**
* Assert icon's meta tags do not appear in HTML
*
* @return void
*/
private function assertAppleTouchIconNotPresent(): void
{
$crawler = self::request('GET', 'index.php');
$this->assertCount(0, $crawler->filter('link[rel="apple-touch-icon"]'));
}

/**
* Assert icon's meta tags appear in HTML
*
* @param string $icon
* @return void
*/
private function assertAppleTouchIconPresent(string $icon): void
{
$crawler = self::request('GET', 'index.php?sid=' . $this->sid);
$this->assertStringContainsString($icon, $crawler->filter('link[rel="apple-touch-icon"]')->attr('href'));
}

/**
* Assert icon does not appear in ACP
*
* @return Crawler
*/
private function assertIconsNotInACP(): Crawler
{
$crawler = self::request('GET', 'adm/index.php?i=-phpbb-pwakit-acp-pwa_acp_module&mode=settings&sid=' . $this->sid);
$this->assertContainsLang('ACP_PWA_KIT_NO_ICONS', $crawler->filter('fieldset')->eq(2)->html());
return $crawler;
}

/**
* Assert icon appears in ACP
*
* @param string $icon
* @return void
*/
private function assertIconInACP(string $icon): void
{
$crawler = self::request('GET', 'adm/index.php?i=-phpbb-pwakit-acp-pwa_acp_module&mode=settings&sid=' . $this->sid);
$this->assertStringContainsString($icon, $crawler->filter('fieldset')->eq(2)->text());
}
}
15 changes: 12 additions & 3 deletions tests/unit/ext_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public function enable_test_data(): array
return [
[true, false, 'create-icon-dir'],
[false, false, 'create-icon-dir'],
[false, true, true],
];
}

Expand All @@ -108,17 +109,25 @@ public function test_enable($file_exists, $old_state, $expected)
->method('exists')
->willReturn($file_exists);

$filesystem->expects($file_exists ? self::never() : self::once())
$filesystem->expects($file_exists || $old_state ? self::never() : self::once())
->method('mkdir');

$this->container->expects(self::once())
$this->extension_finder->expects($old_state ? self::once() : self::never())
->method('extension_directory')
->willReturnSelf();

$this->extension_finder->expects($old_state ? self::once() : self::never())
->method('find_from_extension')
->willReturn([]);

$this->container->expects($old_state ? self::never() : self::once())
->method('get')
->with('filesystem')
->willReturn($filesystem);

$ext = new ext($this->container, $this->extension_finder, $this->migrator, 'phpbb/pwakit', '');

self::assertEquals($expected, $ext->enable_step($old_state));
self::assertSame($expected, $ext->enable_step($old_state));
}

public function test_enable_fails()
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/helper_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ public function test_get_icons()
$this->assertEquals($expected, $this->helper->get_icons());
}

public function test_get_icons_empty()
{
// delete physical foo.png file
@unlink(self::FIXTURES . 'site_icons/foo.png');

$this->assertCount(0, array_column($this->helper->get_icons(), 'src'));
}

public function delete_icon_test_data(): array
{
return [
Expand Down

0 comments on commit 76849b9

Please sign in to comment.