'; private const BUTTON = '' . ''; private static function page(string $buttonAttrs): string { return sprintf(self::BUTTON, $buttonAttrs); } public function testModulePresetInTheStack(): void { $matcher = new PresetUsageMatcher(); $button = new PresetScope('module', 'divi/button'); self::assertTrue($matcher->uses(self::page('{"modulePreset":["aaaaaaaaaa"]}'), $button, 'aaaaaaaaaa', false)); self::assertTrue($matcher->uses(self::page('{"modulePreset":["x","aaaaaaaaaa"]}'), $button, 'aaaaaaaaaa', false)); self::assertTrue($matcher->uses(self::page('{"modulePreset":"aaaaaaaaaa"}'), $button, 'aaaaaaaaaa', false)); self::assertFalse($matcher->uses(self::page('{"modulePreset":["bbbbbbbbbb"]}'), $button, 'aaaaaaaaaa', false)); self::assertFalse( $matcher->uses(self::page('{"modulePreset":["aaaaaaaaaa"]}'), new PresetScope('module', 'divi/text'), 'aaaaaaaaaa', false), 'an ID of another module does not count' ); } public function testDefaultModulePresetReachesModulesWithoutOwnPreset(): void { $matcher = new PresetUsageMatcher(); $button = new PresetScope('module', 'divi/button'); self::assertTrue($matcher->uses(self::page('{}'), $button, 'dddddddddd', true)); self::assertTrue($matcher->uses(self::page('{"modulePreset":["default"]}'), $button, 'dddddddddd', true)); self::assertFalse($matcher->uses(self::page('{"modulePreset":["aaaaaaaaaa"]}'), $button, 'dddddddddd', true)); } public function testGroupPresetById(): void { $matcher = new PresetUsageMatcher(); $group = new PresetScope('group', 'divi/font-body'); $attrs = '{"groupPreset":{"content.decoration.bodyFont":{"presetId":["gggggggggg"],"groupName":"divi/font-body"}}}'; self::assertTrue($matcher->uses(self::page($attrs), $group, 'gggggggggg', false)); self::assertFalse($matcher->uses(self::page($attrs), new PresetScope('group', 'divi/spacing'), 'gggggggggg', false)); } public function testBrokenMarkupCountsAsUse(): void { self::assertTrue((new PresetUsageMatcher())->uses( '', new PresetScope('module', 'divi/button'), 'aaaaaaaaaa', false )); } private static function groupsOf(string $module): array { return $module === 'divi/text' ? ['content.decoration.bodyFont' => 'divi/font-body'] : []; } public function testDefaultGroupPresetReachesModulesWithTheGroup(): void { $matcher = new PresetUsageMatcher(groupsOf: self::groupsOf(...)); $group = new PresetScope('group', 'divi/font-body'); $own = '{"groupPreset":{"content.decoration.bodyFont":{"presetId":["%s"],"groupName":"divi/font-body"}}}'; self::assertTrue($matcher->uses(sprintf(self::TEXT, '{}'), $group, 'gggggggggg', true)); self::assertTrue( $matcher->uses(sprintf(self::TEXT, sprintf($own, 'default')), $group, 'gggggggggg', true), 'an entry with the default ID is no own preset' ); self::assertFalse( $matcher->uses(sprintf(self::TEXT, sprintf($own, 'hhhhhhhhhh')), $group, 'gggggggggg', true), 'an own group preset replaces the default' ); self::assertFalse( $matcher->uses(self::page('{}'), $group, 'gggggggggg', true), 'a button has no font-body group' ); self::assertFalse( $matcher->uses(sprintf(self::TEXT, '{}'), new PresetScope('group', 'divi/spacing'), 'gggggggggg', true), 'the text module has no spacing group in this fixture' ); self::assertFalse( $matcher->uses(sprintf(self::TEXT, '{}'), $group, 'gggggggggg', false), 'a preset that is not the default needs an explicit entry' ); } public function testDefaultGroupPresetIsNotDetectedWithoutGroupsOf(): void { self::assertFalse((new PresetUsageMatcher())->uses( sprintf(self::TEXT, '{}'), new PresetScope('group', 'divi/font-body'), 'gggggggggg', true )); } public function testGlobalInstancesDoNotReachADefaultGroupPreset(): void { $matcher = new PresetUsageMatcher(groupsOf: static fn (string $module): array => [ 'content.decoration.bodyFont' => 'divi/font-body', ]); $instance = ''; self::assertFalse($matcher->uses($instance, new PresetScope('group', 'divi/font-body'), 'gggggggggg', true)); } }