/** * @see https://groups.google.com/forum/#!topic/silverstripe-dev/yHcluCvuszo */ public function testTableDiff() { if (!class_exists('DOMDocument')) { $this->markTestSkipped('"DOMDocument" required'); return; } $from = "<table>\n\t\t<tbody>\n\t\t\t<tr class=\"blah\">\n\t\t\t\t<td colspan=\"2\">Row 1</td>\n\t\t\t</tr>\n\t\t\t<tr class=\"foo\">\n\t\t\t\t<td>Row 2</td>\n\t\t\t\t<td>Row 2</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>Row 3</td>\n\t\t\t\t<td>Row 3</td>\n\t\t\t</tr>\n\t\t\t</tbody>\n\t\t</table>"; $to = "<table class=\"new-class\">\n\t\t<tbody>\n\t\t\t<tr class=\"blah\">\n\t\t\t\t<td colspan=\"2\">Row 1</td>\n\t\t\t</tr>\n\t\t\t<tr class=\"foo\">\n\t\t\t\t<td>Row 2</td>\n\t\t\t\t<td>Row 2</td>\n\t\t\t</tr>\n\t\t</tbody>\n\t\t</table>"; $expected = "<ins>" . $to . "</ins>" . "<del>" . $from . "</del>"; $compare = Diff::compareHTML($from, $to); // 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); $this->assertEquals($compare, $expected); }
/** * Get a SS_List of the changed fields. * Each element is an array data containing * - Name: The field name * - Title: The human-readable field title * - Diff: An HTML diff showing the changes * - From: The older version of the field * - To: The newer version of the field */ public function ChangedFields() { $changedFields = new ArrayList(); if ($this->fromRecord) { $base = $this->fromRecord; $fields = array_keys($this->fromRecord->toMap()); } else { $base = $this->toRecord; $fields = array_keys($this->toRecord->toMap()); } foreach ($fields as $field) { if (in_array($field, $this->ignoredFields)) { continue; } if (!$this->fromRecord || $this->fromRecord->{$field} != $this->toRecord->{$field}) { // Only show HTML diffs for fields which allow HTML values in the first place $fieldObj = $this->toRecord->dbObject($field); if ($this->fromRecord) { $fieldDiff = Diff::compareHTML($this->fromRecord->{$field}, $this->toRecord->{$field}, !$fieldObj || $fieldObj->stat('escape_type') != 'xml'); } else { if ($fieldObj && $fieldObj->stat('escape_type') == 'xml') { $fieldDiff = "<ins>" . $this->toRecord->{$field} . "</ins>"; } else { $fieldDiff = "<ins>" . Convert::raw2xml($this->toRecord->{$field}) . "</ins>"; } } $changedFields->push(new ArrayData(array('Name' => $field, 'Title' => $base->fieldLabel($field), 'Diff' => $fieldDiff, 'From' => $this->fromRecord ? $this->fromRecord->{$field} : null, 'To' => $this->toRecord ? $this->toRecord->{$field} : null))); } } return $changedFields; }