Skip to content

Commit

Permalink
Improve unit test for ResponseRenderer::setAjax()
Browse files Browse the repository at this point in the history
Also tests the setAjax() method of Header, Footer and Console classes.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed May 31, 2023
1 parent b908461 commit 3f587cc
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 49 deletions.
2 changes: 1 addition & 1 deletion libraries/classes/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Console
*
* @var bool
*/
private $isAjax;
private $isAjax = false;

/** @var Relation */
private $relation;
Expand Down
2 changes: 1 addition & 1 deletion libraries/classes/Footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Footer
*
* @var bool
*/
private $isAjax;
private $isAjax = false;
/**
* Whether to only close the BODY and HTML tags
* or also include scripts, errors and links
Expand Down
3 changes: 1 addition & 2 deletions libraries/classes/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Header
*
* @var bool
*/
private $isAjax;
private $isAjax = false;
/**
* Whether to display anything
*
Expand Down Expand Up @@ -107,7 +107,6 @@ public function __construct()
$this->template = new Template();

$this->isEnabled = true;
$this->isAjax = false;
$this->bodyId = '';
$this->title = '';
$this->console = new Console();
Expand Down
8 changes: 0 additions & 8 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -995,11 +995,6 @@
<code>$tabs</code>
</PossiblyNullIterator>
</file>
<file src="libraries/classes/Console.php">
<PropertyNotSetInConstructor occurrences="1">
<code>$isAjax</code>
</PropertyNotSetInConstructor>
</file>
<file src="libraries/classes/Controllers/AbstractController.php">
<MixedArgument occurrences="1">
<code>$db</code>
Expand Down Expand Up @@ -6820,9 +6815,6 @@
<code>array{revision: string, revisionUrl: string, branch: string, branchUrl: string}|[]</code>
<code>is_array($info) ? $info : []</code>
</MixedReturnTypeCoercion>
<PropertyNotSetInConstructor occurrences="1">
<code>$isAjax</code>
</PropertyNotSetInConstructor>
<RedundantCast occurrences="3">
<code>(string) $GLOBALS['db']</code>
<code>(string) $GLOBALS['table']</code>
Expand Down
14 changes: 14 additions & 0 deletions test/classes/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpMyAdmin\Tests;

use PhpMyAdmin\Console;
use ReflectionProperty;

/**
* @covers \PhpMyAdmin\Console
Expand All @@ -16,4 +17,17 @@ public function testGetScripts(): void
$console = new Console();
$this->assertEquals(['console.js'], $console->getScripts());
}

public function testSetAjax(): void
{
$isAjax = new ReflectionProperty(Console::class, 'isAjax');
$isAjax->setAccessible(true);
$console = new Console();

$this->assertFalse($isAjax->getValue($console));
$console->setAjax(true);
$this->assertTrue($isAjax->getValue($console));
$console->setAjax(false);
$this->assertFalse($isAjax->getValue($console));
}
}
19 changes: 15 additions & 4 deletions test/classes/FooterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ArrayIterator;
use PhpMyAdmin\ErrorHandler;
use PhpMyAdmin\Footer;
use ReflectionProperty;

use function json_encode;

Expand Down Expand Up @@ -116,10 +117,7 @@ public function testDisable(): void
);
}

/**
* Test for footer when ajax enabled
*/
public function testAjax(): void
public function testGetDisplayWhenAjaxIsEnabled(): void
{
$footer = new Footer();
$footer->setAjax(true);
Expand Down Expand Up @@ -167,4 +165,17 @@ public function testMinimal(): void
$footer->getDisplay()
);
}

public function testSetAjax(): void
{
$isAjax = new ReflectionProperty(Footer::class, 'isAjax');
$isAjax->setAccessible(true);
$footer = new Footer();

$this->assertFalse($isAjax->getValue($footer));
$footer->setAjax(true);
$this->assertTrue($isAjax->getValue($footer));
$footer->setAjax(false);
$this->assertFalse($isAjax->getValue($footer));
}
}
23 changes: 23 additions & 0 deletions test/classes/HeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PhpMyAdmin\Tests;

use PhpMyAdmin\Console;
use PhpMyAdmin\Core;
use PhpMyAdmin\Header;
use ReflectionProperty;
Expand Down Expand Up @@ -245,4 +246,26 @@ public function providerForTestGetHttpHeaders(): array
],
];
}

public function testSetAjax(): void
{
$header = new Header();
$consoleReflection = new ReflectionProperty(Header::class, 'console');
$consoleReflection->setAccessible(true);
$console = $consoleReflection->getValue($header);
$this->assertInstanceOf(Console::class, $console);
$isAjax = new ReflectionProperty(Header::class, 'isAjax');
$isAjax->setAccessible(true);
$consoleIsAjax = new ReflectionProperty(Console::class, 'isAjax');
$consoleIsAjax->setAccessible(true);

$this->assertFalse($isAjax->getValue($header));
$this->assertFalse($consoleIsAjax->getValue($console));
$header->setAjax(true);
$this->assertTrue($isAjax->getValue($header));
$this->assertTrue($consoleIsAjax->getValue($console));
$header->setAjax(false);
$this->assertFalse($isAjax->getValue($header));
$this->assertFalse($consoleIsAjax->getValue($console));
}
}
59 changes: 59 additions & 0 deletions test/classes/ResponseRendererTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Tests;

use PhpMyAdmin\Footer;
use PhpMyAdmin\Header;
use PhpMyAdmin\ResponseRenderer;
use ReflectionProperty;

/**
* @covers \PhpMyAdmin\ResponseRenderer
*/
class ResponseRendererTest extends AbstractTestCase
{
protected function setUp(): void
{
parent::setUp();

$GLOBALS['lang'] = 'en';
$GLOBALS['server'] = 1;
$GLOBALS['text_dir'] = 'ltr';
$GLOBALS['PMA_PHP_SELF'] = 'index.php';
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testSetAjax(): void
{
$_REQUEST = [];
$response = ResponseRenderer::getInstance();
$header = $response->getHeader();
$footerReflection = new ReflectionProperty(ResponseRenderer::class, 'footer');
$footerReflection->setAccessible(true);
$footer = $footerReflection->getValue($response);
$this->assertInstanceOf(Footer::class, $footer);
$headerIsAjax = new ReflectionProperty(Header::class, 'isAjax');
$headerIsAjax->setAccessible(true);
$footerIsAjax = new ReflectionProperty(Footer::class, 'isAjax');
$footerIsAjax->setAccessible(true);

$this->assertFalse($response->isAjax());
$this->assertFalse($headerIsAjax->getValue($header));
$this->assertFalse($footerIsAjax->getValue($footer));

$response->setAjax(true);
$this->assertTrue($response->isAjax());
$this->assertTrue($headerIsAjax->getValue($header));
$this->assertTrue($footerIsAjax->getValue($footer));

$response->setAjax(false);
$this->assertFalse($response->isAjax());
$this->assertFalse($headerIsAjax->getValue($header));
$this->assertFalse($footerIsAjax->getValue($footer));
}
}
33 changes: 0 additions & 33 deletions test/classes/ResponseTest.php

This file was deleted.

0 comments on commit 3f587cc

Please sign in to comment.