From 5aaa843a2847ec73efd3153fd0b80ac76674aad1 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Wed, 20 Sep 2023 14:39:45 -0700 Subject: [PATCH] tests: Enforce parity between Gadget::serializeDefinition and toArray Follows-up 087ab65e24 (Ieae6706537). To avoid silent mistakes such as during development of T63007 (I7f797e35352b242). Bug: T303194 Change-Id: If675f8d02ed3fee768af3d2b4912249319ae9ef4 --- includes/Gadget.php | 59 ++++++++++++++++--------------- tests/phpunit/unit/GadgetTest.php | 6 ++++ 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/includes/Gadget.php b/includes/Gadget.php index a7c2b298..5aa735fb 100644 --- a/includes/Gadget.php +++ b/includes/Gadget.php @@ -116,24 +116,24 @@ class Gadget { return 'Gadget:' . $page; }; return [ - 'name' => $id, - 'resourceLoaded' => true, - 'requiresES6' => $data['settings']['requiresES6'], - 'requiredRights' => $data['settings']['rights'], - 'onByDefault' => $data['settings']['default'], - 'package' => $data['settings']['package'], - 'hidden' => $data['settings']['hidden'], - 'targets' => $data['settings']['targets'], - 'requiredActions' => $data['settings']['actions'], - 'requiredSkins' => $data['settings']['skins'], 'category' => $data['settings']['category'], - 'supportsUrlLoad' => $data['settings']['supportsUrlLoad'], - 'scripts' => array_map( $prefixGadgetNs, $data['module']['scripts'] ), - 'styles' => array_map( $prefixGadgetNs, $data['module']['styles'] ), 'datas' => array_map( $prefixGadgetNs, $data['module']['datas'] ), 'dependencies' => $data['module']['dependencies'], - 'peers' => $data['module']['peers'], + 'hidden' => $data['settings']['hidden'], 'messages' => $data['module']['messages'], + 'name' => $id, + 'onByDefault' => $data['settings']['default'], + 'package' => $data['settings']['package'], + 'peers' => $data['module']['peers'], + 'requiredActions' => $data['settings']['actions'], + 'requiredRights' => $data['settings']['rights'], + 'requiredSkins' => $data['settings']['skins'], + 'requiresES6' => $data['settings']['requiresES6'], + 'resourceLoaded' => true, + 'scripts' => array_map( $prefixGadgetNs, $data['module']['scripts'] ), + 'styles' => array_map( $prefixGadgetNs, $data['module']['styles'] ), + 'supportsUrlLoad' => $data['settings']['supportsUrlLoad'], + 'targets' => $data['settings']['targets'], 'type' => $data['module']['type'], ]; } @@ -144,26 +144,27 @@ class Gadget { */ public function toArray(): array { return [ - 'name' => $this->name, - 'definition' => $this->definition, - 'resourceLoaded' => $this->resourceLoaded, - 'requiresES6' => $this->requiresES6, - 'requiredRights' => $this->requiredRights, - 'onByDefault' => $this->onByDefault, - 'package' => $this->package, - 'hidden' => $this->hidden, - 'requiredActions' => $this->requiredActions, - 'requiredSkins' => $this->requiredSkins, 'category' => $this->category, - 'supportsUrlLoad' => $this->supportsUrlLoad, - 'scripts' => $this->scripts, - 'styles' => $this->styles, 'datas' => $this->datas, 'dependencies' => $this->dependencies, - 'peers' => $this->peers, + 'hidden' => $this->hidden, 'messages' => $this->messages, - 'type' => $this->type, + 'name' => $this->name, + 'onByDefault' => $this->onByDefault, + 'package' => $this->package, + 'peers' => $this->peers, + 'requiredActions' => $this->requiredActions, + 'requiredRights' => $this->requiredRights, + 'requiredSkins' => $this->requiredSkins, + 'requiresES6' => $this->requiresES6, + 'resourceLoaded' => $this->resourceLoaded, + 'scripts' => $this->scripts, + 'styles' => $this->styles, + 'supportsUrlLoad' => $this->supportsUrlLoad, 'targets' => $this->targets, + 'type' => $this->type, + // Legacy (specific to MediaWikiGadgetsDefinitionRepo) + 'definition' => $this->definition, ]; } diff --git a/tests/phpunit/unit/GadgetTest.php b/tests/phpunit/unit/GadgetTest.php index 45474afc..6c27abcd 100644 --- a/tests/phpunit/unit/GadgetTest.php +++ b/tests/phpunit/unit/GadgetTest.php @@ -58,6 +58,12 @@ class GadgetTest extends MediaWikiUnitTestCase { $this->assertCount( 0, $g->getJSONs() ); $this->assertCount( 1, $g->getDependencies() ); $this->assertCount( 1, $g->getMessages() ); + + // Ensure parity and internal consistency + // between Gadget::serializeDefinition and Gadget::toArray + $arr = $g->toArray(); + unset( $arr['definition'] ); + $this->assertSame( $arr, $gArray ); } /**