Skip to content

Commit

Permalink
Prevent undefined warning
Browse files Browse the repository at this point in the history
  • Loading branch information
oakesjosh committed Aug 12, 2024
1 parent 6c88587 commit cf91d3d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 5 deletions.
6 changes: 1 addition & 5 deletions includes/class-kadence-blocks-css.php
Original file line number Diff line number Diff line change
Expand Up @@ -1273,11 +1273,7 @@ public function render_gap( $attributes, $name = 'gap', $property = 'gap', $unit

$name = $args['desktop_key'] ? $args['desktop_key'] : $name;

$attribute = '';
$attributeTablet = '';
$attributeMobile = '';

if ( is_array( $attributes[ $name ] ) ) {
if ( isset( $attributes[ $name ] ) && is_array( $attributes[ $name ] ) ) {
$attribute = isset( $attributes[ $name ][0] ) ? $attributes[ $name ][0] : '';
$attributeTablet = isset( $attributes[ $name ][1] ) ? $attributes[ $name ][1] : '';
$attributeMobile = isset( $attributes[ $name ][2] ) ? $attributes[ $name ][2] : '';
Expand Down
80 changes: 80 additions & 0 deletions tests/wpunit/KadenceBlocksCssTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,86 @@ public function testGetMediaQueries() {
'Assert mobile media query is filterable' );
}

public function testRenderGap() {
// Test with empty attributes
$result = $this->css->render_gap([], 'gap', 'gap', 'gapUnit');
$this->assertFalse($result, 'Should return false when attributes are empty.');

// Test with non-array attributes
$result = $this->css->render_gap('not-an-array', 'gap', 'gap', 'gapUnit');
$this->assertFalse($result, 'Should return false when attributes is not an array.');

// Test with valid attributes and default unit
$attributes = [
'gap' => [10, 20, 30],
];
$this->css->render_gap($attributes, 'gap', 'gap', 'gapUnit');

$output = $this->css->css_output();
$this->assertStringContainsString('10px', $output, 'Should contain the correct gap for desktop.');
$this->assertStringContainsString('20px', $output, 'Should contain the correct gap for tablet.');
$this->assertStringContainsString('30px', $output, 'Should contain the correct gap for mobile.');

// Test with a different unit
$attributes = [
'gap' => [10, 20, 30],
'gapUnit' => 'em',
];
$this->css->render_gap($attributes, 'gap', 'gap', 'gapUnit');

$output = $this->css->css_output();
$this->assertStringContainsString('10em', $output, 'Should contain the correct gap for desktop with unit.');
$this->assertStringContainsString('20em', $output, 'Should contain the correct gap for tablet with unit.');
$this->assertStringContainsString('30em', $output, 'Should contain the correct gap for mobile with unit.');

// Test with args overriding the default name
$attributes = [
'customGap' => [15, 25, 35],
];
$args = [
'desktop_key' => 'customGap',
'tablet_key' => 'customGap',
'mobile_key' => 'customGap',
];
$this->css->render_gap($attributes, 'gap', 'gap', 'gapUnit', $args);

$output = $this->css->css_output();
$this->assertStringContainsString('15px', $output, 'Should contain the correct gap for desktop using custom key.');
$this->assertStringContainsString('25px', $output, 'Should contain the correct gap for tablet using custom key.');
$this->assertStringContainsString('35px', $output, 'Should contain the correct gap for mobile using custom key.');

// Test with missing middle value
$attributes = [
'gap' => [10, '', 30],
'gapUnit' => 'rem',
];
$this->css->render_gap($attributes, 'gap', 'gap', 'gapUnit');

$output = $this->css->css_output();
$this->assertStringContainsString('10rem', $output, 'Should contain the correct gap for desktop with unit.');
$this->assertStringContainsString('30rem', $output, 'Should contain the correct gap for mobile with unit.');
$this->assertStringNotContainsString('20rem', $output, 'Should not contain a gap for tablet when it is empty.');

// Test with attributes that are not arrays (e.g., direct values for keys)
$attributes = [
'desktopGap' => '15',
'tabletGap' => '25',
'mobileGap' => '35',
'gapUnit' => 'px',
];
$args = [
'desktop_key' => 'desktopGap',
'tablet_key' => 'tabletGap',
'mobile_key' => 'mobileGap',
];
$this->css->render_gap($attributes, 'gap', 'gap', 'gapUnit', $args);

$output = $this->css->css_output();
$this->assertStringContainsString('15px', $output, 'Should contain the correct gap for desktop from individual attributes.');
$this->assertStringContainsString('25px', $output, 'Should contain the correct gap for tablet from individual attributes.');
$this->assertStringContainsString('35px', $output, 'Should contain the correct gap for mobile from individual attributes.');
}

protected function _before() {
$this->css = new Kadence_Blocks_CSS();
}
Expand Down

0 comments on commit cf91d3d

Please sign in to comment.