Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test-case for Fix parameter output if parameter is Datetime or array [v1.22.4] #671 #673

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified chromedriver
Binary file not shown.
3 changes: 2 additions & 1 deletion demo/bridge/doctrine/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@

$product = new Demo\Product();
$product->setName("foobar");

$product->setUpdated();

$entityManager->persist($product);
$entityManager->flush();
$entityManager->createQuery("select p from Demo\\Product p where p.updated>:u")->setParameter("u", new \DateTime('1 hour ago'))->execute();
$entityManager->createQuery("select p from Demo\\Product p where p.name=:c")->setParameter("c", "<script>alert();</script>")->execute();
render_demo_page();
8 changes: 8 additions & 0 deletions demo/bridge/doctrine/src/Demo/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Product
protected $id;
/** @Column(type="string") **/
protected $name;
/** @Column(type="datetime", nullable=true) **/
protected $updated;

public function getId()
{
Expand All @@ -26,4 +28,10 @@ public function setName($name)
{
$this->name = $name;
}

public function setUpdated(): void
{
// will NOT be saved in the database
$this->updated = new \DateTime('now');
}
}
15 changes: 10 additions & 5 deletions src/DebugBar/Bridge/DoctrineCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,16 @@ public function collect()
*/
public function getParameters($query) : array
{
$params = [];
foreach ($query['params'] ?? [] as $name => $param) {
$params[$name] = htmlentities($param?:"", ENT_QUOTES, 'UTF-8', false);
}
return $params;
return array_map(function ($param) {
if (is_string($param)) {
return htmlentities($param, ENT_QUOTES, 'UTF-8', false);
} elseif (is_array($param)) {
return implode(', ', $param);
} elseif ($param instanceof \DateTimeInterface) {
return $param->format('Y-m-d H:i:s');
}
return $param ?: '';
}, $query['params'] ?? []);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/DebugBar/Tests/Browser/Bridge/DoctrineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public function testMonologCollector(): void
return $node->getText();
});

$this->assertEquals('INSERT INTO products (name) VALUES (?)', $statements[1]);
$this->assertCount(4, $statements);
$this->assertEquals('INSERT INTO products (name, updated) VALUES (?, ?)', $statements[1]);
$this->assertCount(5, $statements);
}

}