Fix template validation type mismatch causing "template not available" error#353
Fix template validation type mismatch causing "template not available" error#353superdav42 wants to merge 1 commit intomainfrom
Conversation
…_templates Fixes #351 - Template validation failing with 'The selected template is not available for this product' error. Root cause: Template IDs stored as string keys in the limitations array were not being converted to integers, causing type mismatch with the validation rule which uses absint() on the submitted template ID. Changes: - Updated get_available_site_templates() to convert site_id to integer - Updated get_pre_selected_site_template() to convert site_id to integer - Added regression test to ensure template IDs are returned as integers - Test validates strict type checking with in_array(..., true) This ensures consistent integer comparison in the Site_Template validation rule (inc/helpers/validation-rules/class-site-template.php:104).
📝 WalkthroughWalkthroughThis pull request fixes a type consistency issue in site template handling by converting site IDs to integers using Changes
Poem
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
🔨 Build Complete - Ready for Testing!📦 Download Build Artifact (Recommended)Download the zip build, upload to WordPress and test:
🌐 Test in WordPress Playground (Very Experimental)Click the link below to instantly test this PR in your browser - no installation needed! Login credentials: |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/WP_Ultimo/Objects/Limitations_Test.php (1)
921-950: Add an assertion forget_pre_selected_site_template()to cover the second production change.This test already covers
get_available_site_templates()well; adding one assertion for the pre-selected path closes the remaining regression gap.✅ Suggested test addition
public function test_available_site_templates_returns_integers(): void { $limitations = new Limitations([ 'site_templates' => [ 'enabled' => true, 'mode' => 'choose_available_templates', 'limit' => [ '123' => ['behavior' => 'available'], '456' => ['behavior' => 'pre_selected'], '789' => ['behavior' => 'not_available'], ], ], ]); $available = $limitations->site_templates->get_available_site_templates(); + $pre_selected = $limitations->site_templates->get_pre_selected_site_template(); // Should return integers, not strings $this->assertContains(123, $available, 'Template 123 should be in available array as integer'); $this->assertContains(456, $available, 'Template 456 should be in available array as integer'); $this->assertNotContains(789, $available, 'Template 789 should not be available'); + $this->assertSame(456, $pre_selected, 'Pre-selected template ID should be returned as integer'); // Verify strict type checking foreach ($available as $template_id) { $this->assertIsInt($template_id, 'All template IDs should be integers'); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/WP_Ultimo/Objects/Limitations_Test.php` around lines 921 - 950, Add an assertion in test_available_site_templates_returns_integers to cover the pre-selected path: call $limitations->site_templates->get_pre_selected_site_template() and assert it returns the expected template ID (456) as an integer (use assertSame or assertIsInt + assertEquals) so the pre-selected code path is exercised and the returned ID is strictly typed.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@tests/WP_Ultimo/Objects/Limitations_Test.php`:
- Around line 921-950: Add an assertion in
test_available_site_templates_returns_integers to cover the pre-selected path:
call $limitations->site_templates->get_pre_selected_site_template() and assert
it returns the expected template ID (456) as an integer (use assertSame or
assertIsInt + assertEquals) so the pre-selected code path is exercised and the
returned ID is strictly typed.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 6b37777b-980f-4ea1-86b9-6e2cdbc7038c
📒 Files selected for processing (2)
inc/limitations/class-limit-site-templates.phptests/WP_Ultimo/Objects/Limitations_Test.php
Summary
absint()on the submitted template IDget_available_site_templates()andget_pre_selected_site_template()methodsChanges
inc/limitations/class-limit-site-templates.php:
absint($site_id)inget_available_site_templates()absint($site_id)inget_pre_selected_site_template()tests/WP_Ultimo/Objects/Limitations_Test.php:
test_available_site_templates_returns_integers()regression testin_array(..., true)Technical Details
The issue occurs because:
['123' => ['behavior' => 'available']])inc/helpers/validation-rules/class-site-template.php:50converts the submitted template ID to integer usingabsint()in_array($template_id, $allowed_templates)which can fail with string/int mismatchesEven though
in_array()without strict mode should handle string/int comparison, there are edge cases where this fails, particularly after array merging operations.Testing
test_available_site_templates_returns_integers()Related
@commercial-hippie Could you please test this fix on your site? This should resolve the "The selected template is not available for this product" error you're experiencing.
Summary by CodeRabbit
Bug Fixes
Tests