2011-09-12 17:50:02 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\View\Tests\Parsers;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\View\Parsers\Diff;
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class DiffTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://groups.google.com/forum/#!topic/silverstripe-dev/yHcluCvuszo
|
|
|
|
*/
|
|
|
|
public function testTableDiff()
|
|
|
|
{
|
|
|
|
if (!class_exists('DOMDocument')) {
|
|
|
|
$this->markTestSkipped('"DOMDocument" required');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$from = "<table>
|
2014-08-15 08:53:05 +02:00
|
|
|
<tbody>
|
|
|
|
<tr class=\"blah\">
|
|
|
|
<td colspan=\"2\">Row 1</td>
|
|
|
|
</tr>
|
|
|
|
<tr class=\"foo\">
|
|
|
|
<td>Row 2</td>
|
|
|
|
<td>Row 2</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>Row 3</td>
|
|
|
|
<td>Row 3</td>
|
|
|
|
</tr>
|
|
|
|
</tbody>
|
2011-09-12 17:50:02 +02:00
|
|
|
</table>";
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$to = "<table class=\"new-class\">
|
2014-08-15 08:53:05 +02:00
|
|
|
<tbody>
|
|
|
|
<tr class=\"blah\">
|
|
|
|
<td colspan=\"2\">Row 1</td>
|
|
|
|
</tr>
|
|
|
|
<tr class=\"foo\">
|
|
|
|
<td>Row 2</td>
|
|
|
|
<td>Row 2</td>
|
|
|
|
</tr>
|
|
|
|
</tbody>
|
2011-09-12 17:50:02 +02:00
|
|
|
</table>";
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$expected = "<ins>" . $to . "</ins>" . "<del>" . $from . "</del>";
|
|
|
|
$compare = Diff::compareHTML($from, $to);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// Very hard to debug this way, wouldn't need to do this if PHP had an *actual* DOM parsing lib,
|
|
|
|
// and not just the poor excuse that is DOMDocument
|
|
|
|
$compare = preg_replace('/[\s\t\n\r]*/', '', $compare);
|
|
|
|
$expected = preg_replace('/[\s\t\n\r]*/', '', $expected);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2018-06-01 01:16:03 +02:00
|
|
|
$this->assertEquals($expected, $compare);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://github.com/silverstripe/silverstripe-framework/issues/8053
|
|
|
|
*/
|
|
|
|
public function testLegacyEachStatement()
|
|
|
|
{
|
|
|
|
$sentenceOne =
|
|
|
|
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
|
|
|
|
$sentenceTwo =
|
|
|
|
'Nulla porttitor, ex quis commodo pharetra, diam dui efficitur justo, eu gravida elit eros vel libero.';
|
|
|
|
|
|
|
|
$from = "$sentenceOne $sentenceTwo";
|
|
|
|
$to = "$sentenceTwo $sentenceOne";
|
|
|
|
|
2018-06-01 03:02:49 +02:00
|
|
|
// We're cheating our test a little bit here, because depending on what HTML cleaner you have, you'll get
|
|
|
|
// spaces added or not added around the tags.
|
|
|
|
$expected = "/^ *<del>$sentenceOne<\/del> *$sentenceTwo *<ins>$sentenceOne<\/ins> *$/";
|
2018-06-01 01:16:03 +02:00
|
|
|
$actual = Diff::compareHTML($from, $to);
|
|
|
|
|
2018-06-01 03:02:49 +02:00
|
|
|
$this->assertRegExp($expected, $actual);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2020-03-17 03:57:28 +01:00
|
|
|
|
|
|
|
public function testDiffArray()
|
|
|
|
{
|
|
|
|
$from = ['Lorem', ['array here please ignore'], 'ipsum dolor'];
|
|
|
|
$to = 'Lorem,ipsum';
|
|
|
|
$expected = "/^Lorem,ipsum *<del>dolor<\/del> *$/";
|
|
|
|
$actual = Diff::compareHTML($from, $to);
|
|
|
|
|
|
|
|
$this->assertRegExp($expected, $actual);
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|