BUG Allow Email to re-render when data changes (#9876)

* Fix: Allow Email to re-render when data changes

* Add invalidateBody function

* Make the linter happy
This commit is contained in:
Nik 2021-03-03 23:18:46 +01:00 committed by GitHub
parent 705b746d0b
commit d2fa64b489
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 0 deletions

View File

@ -570,6 +570,7 @@ class Email extends ViewableData
public function setData($data)
{
$this->data = $data;
$this->invalidateBody();
return $this;
}
@ -589,6 +590,8 @@ class Email extends ViewableData
$this->data->$name = $value;
}
$this->invalidateBody();
return $this;
}
@ -606,6 +609,8 @@ class Email extends ViewableData
$this->data->$name = null;
}
$this->invalidateBody();
return $this;
}
@ -635,6 +640,16 @@ class Email extends ViewableData
return $this;
}
/**
* @return $this
*/
public function invalidateBody()
{
$this->setBody(null);
return $this;
}
/**
* @return string The base URL for the email
*/

View File

@ -559,6 +559,39 @@ class EmailTest extends SapphireTest
$this->assertCount(1, $email->getSwiftMessage()->getChildren());
}
public function testRerender()
{
$email = new Email();
$email->setData([
'EmailContent' => 'my content',
]);
$email->render();
$this->assertContains('my content', $email->getBody());
$children = $email->getSwiftMessage()->getChildren();
$this->assertCount(1, $children);
$plainPart = reset($children);
$this->assertEquals('my content', $plainPart->getBody());
// Ensure setting data causes a rerender
$email->setData([
'EmailContent' => 'your content'
]);
$email->render();
$this->assertContains('your content', $email->getBody());
// Ensure removing data causes a rerender
$email->removeData('EmailContent');
$email->render();
$this->assertNotContains('your content', $email->getBody());
// Ensure adding data causes a rerender
$email->addData([
'EmailContent' => 'their content'
]);
$email->render();
$this->assertContains('their content', $email->getBody());
}
public function testRenderPlainOnly()
{
$email = new Email();