wp-github-updater/tests/Unit/UpdaterEnqueueScriptTest.php
Copilot 0b20058812
Feature: Built-in "Check Updates" JavaScript eliminates per-plugin duplication (#13)
* Initial plan

* Add JavaScript asset and PHP methods for built-in check-updates functionality

Co-authored-by: miguelcolmenares <1714344+miguelcolmenares@users.noreply.github.com>

* Update documentation and version to 1.3.0 for check-updates feature

Co-authored-by: miguelcolmenares <1714344+miguelcolmenares@users.noreply.github.com>

* Add v1.3.0 usage examples to integration guide

Co-authored-by: miguelcolmenares <1714344+miguelcolmenares@users.noreply.github.com>

* fix: Address all 7 Copilot PR review comments

- Fix XSS vulnerability in showAdminNotice() using DOM construction
  instead of template string interpolation (#discussion_r2873337739)
- Add 'dismissNotice' to i18n strings making dismiss button text
  translatable (#discussion_r2873337786)
- Replace deprecated assertRegExp()/assertNotRegExp() with
  assertMatchesRegularExpression()/assertDoesNotMatchRegularExpression()
  for PHPUnit 9.6 (#discussion_r2873337810, #discussion_r2873337822)
- Fix site_url() mock fatal error: change parameter type from
  'string' to '?string' for nullable default (#discussion_r2873337870)
- Rewrite getPackageAssetUrl() to use plugin_dir_url() instead of
  ABSPATH replacement, supporting subdirectory WP installs
  (#discussion_r2873337887)
- Mark getPackageAssetUrl()/sanitizeJsVarName() as @internal in
  PHPDoc to clarify they are not public API (#discussion_r2873337845)
- Add plugin_dir_url() mock for test compatibility

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: miguelcolmenares <1714344+miguelcolmenares@users.noreply.github.com>
Co-authored-by: Miguel Colmenares <me@miguelcolmenares.com>
2026-03-02 11:29:03 -05:00

102 lines
3.4 KiB
PHP

<?php
namespace SilverAssist\WpGithubUpdater\Tests\Unit;
use PHPUnit\Framework\TestCase;
use SilverAssist\WpGithubUpdater\Updater;
use SilverAssist\WpGithubUpdater\UpdaterConfig;
/**
* Test the enqueueCheckUpdatesScript functionality
*
* @package SilverAssist\WpGithubUpdater\Tests\Unit
* @since 1.3.0
*/
class UpdaterEnqueueScriptTest extends TestCase
{
private static string $testPluginFile;
public static function setUpBeforeClass(): void
{
self::$testPluginFile = dirname(__DIR__) . "/fixtures/test-plugin.php";
}
public function testEnqueueCheckUpdatesScriptReturnsValidJavaScript(): void
{
$config = new UpdaterConfig(self::$testPluginFile, "owner/repo", [
"plugin_name" => "Test Plugin",
"text_domain" => "test-plugin",
]);
$updater = new Updater($config);
$result = $updater->enqueueCheckUpdatesScript();
// Should return a string
$this->assertIsString($result);
// Should contain the function call
$this->assertStringContainsString("wpGithubUpdaterCheckUpdates", $result);
// Should contain return false
$this->assertStringContainsString("return false", $result);
// Should contain a valid JS variable name (sanitized plugin basename)
$this->assertMatchesRegularExpression("/wpGithubUpdaterCheckUpdates\('[a-zA-Z0-9_$]+\'\)/", $result);
}
public function testEnqueueCheckUpdatesScriptWithExtraStrings(): void
{
$config = new UpdaterConfig(self::$testPluginFile, "owner/repo", [
"plugin_name" => "Test Plugin",
"text_domain" => "test-plugin",
]);
$updater = new Updater($config);
$extraStrings = [
"checking" => "Custom checking message...",
"upToDate" => "Custom up to date message!",
];
$result = $updater->enqueueCheckUpdatesScript($extraStrings);
// Should still return valid JavaScript
$this->assertIsString($result);
$this->assertStringContainsString("wpGithubUpdaterCheckUpdates", $result);
}
public function testEnqueueCheckUpdatesScriptUsesPluginBasename(): void
{
$config = new UpdaterConfig(self::$testPluginFile, "owner/repo", [
"plugin_name" => "Test Plugin",
]);
$updater = new Updater($config);
$result = $updater->enqueueCheckUpdatesScript();
// The result should reference the plugin (test-plugin is the basename)
// Note: The exact value depends on plugin basename extraction from test-plugin.php
$this->assertStringContainsString("wpGithubUpdaterCheckUpdates", $result);
// Should not contain any invalid JavaScript characters in the variable name
$this->assertDoesNotMatchRegularExpression("/wpGithubUpdaterCheckUpdates\('[^a-zA-Z0-9_$']+\'\)/", $result);
}
/**
* Test that the method can be called multiple times without errors
*/
public function testEnqueueCheckUpdatesScriptCanBeCalledMultipleTimes(): void
{
$config = new UpdaterConfig(self::$testPluginFile, "owner/repo");
$updater = new Updater($config);
$result1 = $updater->enqueueCheckUpdatesScript();
$result2 = $updater->enqueueCheckUpdatesScript();
// Both calls should return valid results
$this->assertIsString($result1);
$this->assertIsString($result2);
// Results should be identical
$this->assertEquals($result1, $result2);
}
}