<?xml version="1.0" encoding="UTF-8"?>
<deliverance-test-suite>

<!-- tests replace command successfully replaces a simple div tag --> 
<deliverance-test>
  <rules xmlns="http://www.plone.org/deliverance">
    <replace theme="//div[@id='foo']" content="//div[@id='bar']" />
  </rules>

  <theme base="http://example.com"> 
     <html><head><title>Blah</title></head><body><div id="foo">Dummy Content</div></body></html>
  </theme>

  <content> 
     <html><body><div id="bar">Real Content</div></body></html>
  </content>
 
  <output>
     <html><head><title>Blah</title></head><body><div id="bar">Real Content</div></body></html>
  </output> 
</deliverance-test>


<deliverance-test>
  <rules xmlns="http://www.plone.org/deliverance">
    <replace theme="//div[@id='foo']" content="//div[@id='bar']" />
  </rules>

  <theme base="http://example.com"> 
     <html><head><title>Blah</title></head><body><p>first stuff</p><div id="foo">Dummy Content</div>last stuff</body></html>
  </theme>

  <content> 
     <html><body><div id="bar">Real Content</div><div id="bar">More Real Content</div></body></html>
  </content>
 
  <output>
     <html><head><title>Blah</title></head><body><p>first stuff</p><div id="bar">Real Content</div><div id="bar">More Real Content</div>last stuff</body></html>
  </output> 
</deliverance-test>

<!-- tests that an error is raised if replace command does not find an element --> 
<deliverance-test>
  <rules xmlns="http://www.plone.org/deliverance">
    <replace theme="//div[@id='foo']" content="//div[@id='bar']" />
  </rules>

  <theme base="http://example.com"> 
     <html><head><title>Blah</title></head><body></body></html>
  </theme>

  <content> 
     <html><body><div id="bar">Real Content</div></body></html>
  </content>
 
  <output>
     <html><head><title>Blah</title></head><body><div class="deliverance-error">Deliverance error: no element found in theme<br /> 
        rule: &lt;replace theme="//div[@id='foo']" content="//div[@id='bar']"/&gt;</div> 
     </body></html>
  </output> 
</deliverance-test>


<!-- tests that an error is raised if replace command finds multiple target elements in the theme --> 
<deliverance-test>
  <rules xmlns="http://www.plone.org/deliverance">
    <replace theme="//div[@class='foo']" content="//div[@id='bar']" />
  </rules>

  <theme base="http://example.com"> 
      <html><head><title>Blah</title></head><body><div class="foo">Dummy Content</div><div class="foo">Dummy Content</div></body></html>
  </theme>

  <content> 
     <html><body><div id="bar">Real Content</div></body></html>
  </content>
 
  <output>
<html>
  <head>
    <title>Blah</title>
  </head>
  <body>
    <div class="deliverance-error">Deliverance error: multiple elements found in theme<br/>rule: &lt;replace theme="//div[@class='foo']" content="//div[@id='bar']"/&gt;
  <br/><textarea rows="24" cols="80" readonly="readonly">&lt;div class="foo"&gt;Dummy Content&lt;/div&gt;&lt;div class="foo"&gt;Dummy Content&lt;/div&gt;
</textarea></div>
    <div class="foo">Dummy Content</div>
    <div class="foo">Dummy Content</div>
  </body>
</html>

  </output> 
</deliverance-test>


<!-- tests replacing an element with just text -->
<deliverance-test>
  <rules xmlns="http://www.plone.org/deliverance">
    <replace theme="//div[@id='foo']" content="//div[@id='bar']/child::node()" />
  </rules>

  <theme base="http://example.com"> 
     <html><head><title>Blah</title></head><body><div id="foo">Dummy Content</div></body></html>
  </theme>

  <content> 
     <html><body><div id="bar">Real Content</div></body></html>
  </content>
 
  <output>
     <html><head><title>Blah</title></head><body>Real Content</body></html>
  </output> 
</deliverance-test>

<!-- a more advanced test that replaces an element with a mixture of elements and text -->
<deliverance-test>
  <rules xmlns="http://www.plone.org/deliverance">
    <replace theme="//div[@id='foo']" content="//div[@id='bar']/child::node()" />
  </rules>

  <theme base="http://example.com"> 
     <html><head><title>Blah</title></head><body><div id="foo">Dummy Content</div>theme tail</body></html>
  </theme>

  <content> 
     <html><body><div id="bar">Real Content<p>a nice p tag</p>mid text<i>some i text</i>end tail</div></body></html>
  </content>
 
  <output>
     <html><head><title>Blah</title></head><body>Real Content<p>a nice p tag</p>mid text<i>some i text</i>end tailtheme tail</body></html>
  </output> 
</deliverance-test>


<!-- a test that replaces an element with a mixture of elements and text -->
<deliverance-test>
  <rules xmlns="http://www.plone.org/deliverance">
    <replace theme="//div[@id='foo']" content="//div[@id='bar']/child::node()" />
  </rules>

  <theme base="http://example.com"> 
     <html><head><title>Blah</title></head><body><p>some text</p><div id="foo">Dummy Content</div>theme tail</body></html>
  </theme>

  <content> 
     <html><body><div id="bar">Real Content<p>a nice p tag</p>mid text<i>some i text</i>end tail</div></body></html>
  </content>
 
  <output>
     <html><head><title>Blah</title></head><body><p>some text</p>Real Content<p>a nice p tag</p>mid text<i>some i text</i>end tailtheme tail</body></html>
  </output> 
</deliverance-test>


<!-- tests replace command generates an error when no content is found -->
<deliverance-test>
  <rules xmlns="http://www.plone.org/deliverance">
    <replace theme=".//div[@id='foo']" content="//div[@id='bar']" />
  </rules>

  <theme base="http://example.com"> 
    <html><head><title>Blah</title></head><body><div></div><div id="foo">Dummy Content<p>HI!</p></div>
    </body></html>
  </theme>

  <content> 
    <html><body>leading text <br/> more <p>inside p </p>trailing text</body> tail of body</html>
  </content>
  
  <output>
<html>
  <head>
    <title>Blah</title>
  </head>
  <body>
    <div class="deliverance-error">Deliverance error: no content matched<br/>rule: &lt;replace theme=".//div[@id='foo']" content="//div[@id='bar']"/&gt;
  </div>
    <div/>
    <div id="foo">Dummy Content<p>HI!</p></div>
  </body>
</html>
  </output> 
</deliverance-test>

<!-- tests replace command ignores error when no content is found 
and nocontent='ignore' is set-->
<deliverance-test>
  <rules xmlns="http://www.plone.org/deliverance">
    <replace theme=".//div[@id='foo']" content="//div[@id='bar']" nocontent='ignore' />
  </rules>

  <theme base="http://example.com"> 
    <html><head><title>Blah</title></head><body><div></div><div id="foo">Dummy Content<p>HI!</p></div>
    </body></html>
  </theme>

  <content> 
    <html><body>leading text <br/> more <p>inside p </p>trailing text</body> tail of body</html>
  </content>
  
  <output>
<html>
  <head>
    <title>Blah</title>
  </head>
  <body>
    <div/>
    <div id="foo">Dummy Content<p>HI!</p></div>
  </body>
</html>
  </output> 
</deliverance-test>

<!-- tests replace command on edge case: html element  --> 
<deliverance-test>
  <rules xmlns="http://www.plone.org/deliverance">
    <replace theme="//html" content="//html" />
  </rules>

  <theme base="http://example.com"> 
     <html><head><title>Blah</title></head><body><div id="foo">Dummy Content</div></body></html>
  </theme>

  <content> 
     <html><body><div id="bar">Real Content</div></body></html>
  </content>
 
  <output>
     <html><head><title>Blah</title></head><body><div class="deliverance-error">Deliverance error: cannot replace whole theme<br /> 
        rule: &lt;replace theme="//html" content="//html"/&gt;</div><div id="foo">Dummy Content</div> 
     </body></html>
  </output> 

</deliverance-test>


</deliverance-test-suite>

