Skip to content

Commit c381af4

Browse files
committed
Merge pull request #125 from phpcr/lastmodified
add tests for mix:lastModified and handling for implementation loader
2 parents ac8215c + 4518d2a commit c381af4

File tree

5 files changed

+386
-0
lines changed

5 files changed

+386
-0
lines changed

fixtures/10_Writing/lastmodified.xml

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<sv:node xmlns:crx="http://www.day.com/crx/1.0" xmlns:mix="http://www.jcp.org/jcr/mix/1.0"
3+
xmlns:nt="http://www.jcp.org/jcr/nt/1.0" xmlns:fn="http://www.w3.org/2005/xpath-functions"
4+
xmlns:fn_old="http://www.w3.org/2004/10/xpath-functions" xmlns:vlt="http://www.day.com/jcr/vault/1.0"
5+
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jcr="http://www.jcp.org/jcr/1.0"
6+
xmlns:sv="http://www.jcp.org/jcr/sv/1.0" xmlns:rep="internal" sv:name="tests_write_manipulation_add">
7+
<sv:property sv:name="jcr:primaryType" sv:type="Name">
8+
<sv:value>nt:unstructured</sv:value>
9+
</sv:property>
10+
<sv:property sv:name="jcr:created" sv:type="Date">
11+
<sv:value>2009-04-27T13:00:54.082+02:00</sv:value>
12+
</sv:property>
13+
<sv:node sv:name="testCreate">
14+
<sv:property sv:name="jcr:primaryType" sv:type="Name">
15+
<sv:value>nt:unstructured</sv:value>
16+
</sv:property>
17+
</sv:node>
18+
<sv:node sv:name="testCreateManual">
19+
<sv:property sv:name="jcr:primaryType" sv:type="Name">
20+
<sv:value>nt:unstructured</sv:value>
21+
</sv:property>
22+
</sv:node>
23+
<sv:node sv:name="testUpdateText">
24+
<sv:property sv:name="jcr:primaryType" sv:type="Name">
25+
<sv:value>nt:unstructured</sv:value>
26+
</sv:property>
27+
<sv:property sv:name="jcr:mixinTypes" sv:type="Name">
28+
<sv:value>mix:lastModified</sv:value>
29+
</sv:property>
30+
<sv:property sv:name="jcr:lastModified" sv:type="Date">
31+
<sv:value>2011-05-20T08:20:30.108+02:00</sv:value>
32+
</sv:property>
33+
<sv:property sv:name="jcr:lastModifiedBy" sv:type="String">
34+
<sv:value>_phpcr-api-tests</sv:value>
35+
</sv:property>
36+
<sv:property sv:name="text" sv:type="String">
37+
<sv:value>old</sv:value>
38+
</sv:property>
39+
</sv:node>
40+
<sv:node sv:name="testUpdateManual">
41+
<sv:property sv:name="jcr:primaryType" sv:type="Name">
42+
<sv:value>nt:unstructured</sv:value>
43+
</sv:property>
44+
<sv:property sv:name="jcr:mixinTypes" sv:type="Name">
45+
<sv:value>mix:lastModified</sv:value>
46+
</sv:property>
47+
<sv:property sv:name="jcr:lastModified" sv:type="Date">
48+
<sv:value>2011-05-20T08:20:30.108+02:00</sv:value>
49+
</sv:property>
50+
<sv:property sv:name="jcr:lastModifiedBy" sv:type="String">
51+
<sv:value>_phpcr-api-tests</sv:value>
52+
</sv:property>
53+
<sv:property sv:name="text" sv:type="String">
54+
<sv:value>old</sv:value>
55+
</sv:property>
56+
</sv:node>
57+
<sv:node sv:name="testUpdateBinary">
58+
<sv:property sv:name="jcr:primaryType" sv:type="Name">
59+
<sv:value>nt:unstructured</sv:value>
60+
</sv:property>
61+
<sv:property sv:name="jcr:mixinTypes" sv:type="Name">
62+
<sv:value>mix:lastModified</sv:value>
63+
</sv:property>
64+
<sv:property sv:name="jcr:lastModified" sv:type="Date">
65+
<sv:value>2011-05-20T08:20:30.108+02:00</sv:value>
66+
</sv:property>
67+
<sv:property sv:name="jcr:lastModifiedBy" sv:type="String">
68+
<sv:value>_phpcr-api-tests</sv:value>
69+
</sv:property>
70+
<sv:property sv:name="binary-data" sv:type="Binary">
71+
<sv:value>aDEuIENoYXB0ZXIgMSBUaXRsZQoKKiBmb28KKiBiYXIKKiogZm9vMgoqKiBmb28zCiogZm9vMAoKfHwgaGVhZGVyIHx8IGJhciB8fAp8IGggfCBqIHwKCntjb2RlfQpoZWxsbyB3b3JsZAp7Y29kZX0KCiMgZm9vCg==</sv:value>
72+
</sv:property>
73+
</sv:node>
74+
<sv:node sv:name="testRemoveProperty">
75+
<sv:property sv:name="jcr:primaryType" sv:type="Name">
76+
<sv:value>nt:unstructured</sv:value>
77+
</sv:property>
78+
<sv:property sv:name="jcr:mixinTypes" sv:type="Name">
79+
<sv:value>mix:lastModified</sv:value>
80+
</sv:property>
81+
<sv:property sv:name="jcr:lastModified" sv:type="Date">
82+
<sv:value>2011-05-20T08:20:30.108+02:00</sv:value>
83+
</sv:property>
84+
<sv:property sv:name="jcr:lastModifiedBy" sv:type="String">
85+
<sv:value>_phpcr-api-tests</sv:value>
86+
</sv:property>
87+
<sv:property sv:name="text" sv:type="String">
88+
<sv:value>old</sv:value>
89+
</sv:property>
90+
</sv:node>
91+
</sv:node>

inc/AbstractLoader.php

+33
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,39 @@ public function getAdditionalSession($credentials = false)
176176
return $this->getSessionForWorkspace($credentials, $this->getOtherWorkspaceName());
177177
}
178178

179+
/**
180+
* If the implementation can automatically update mix:lastModified nodes,
181+
* this should return a session configured to do that.
182+
*
183+
* Otherwise, the test regarding this feature is skipped.
184+
*
185+
* @return \PHPCR\SessionInterface
186+
*
187+
* @throws \PHPUnit_Framework_SkippedTestSuiteError to make whole test
188+
* suite skip if implementation does not support updating the
189+
* properties automatically.
190+
*/
191+
public function getSessionWithLastModified()
192+
{
193+
if ($this->doesSessionLastModified()) {
194+
return $this->getSession();
195+
}
196+
197+
throw new \PHPUnit_Framework_SkippedTestSuiteError('Not supported');
198+
}
199+
200+
/**
201+
* The implementation loader should provide a session that does not update
202+
* mix:lastModified. If that is not possible, this method should return
203+
* true, which will skip the test about this feature.
204+
*
205+
* @return boolean
206+
*/
207+
public function doesSessionLastModified()
208+
{
209+
return false;
210+
}
211+
179212
/**
180213
* Decide whether this test can be executed.
181214
*

inc/BaseCase.php

+17
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,21 @@ protected function assertEqualDateTime(DateTime $date1, DateTime $date2)
252252
{
253253
$this->assertEquals($date1->getTimestamp(), $date2->getTimestamp());
254254
}
255+
256+
/**
257+
* Assert that both arguments are datetime and are within 3 seconds of each
258+
* other. Use this rather than plain "Equal" when checking application
259+
* generated dates.
260+
*
261+
* @param \DateTime $expected
262+
* @param \DateTime $data
263+
*/
264+
protected function assertSimilarDateTime($expected, $data)
265+
{
266+
$this->assertInstanceOf('\DateTime', $expected);
267+
$this->assertInstanceOf('\DateTime', $data);
268+
$this->assertTrue(abs($expected->getTimestamp() - $data->getTimestamp()) <= 3,
269+
$data->format('c') . ' is not close to the expected ' . $expected->format('c')
270+
);
271+
}
255272
}

tests/10_Writing/LastModifiedTest.php

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
namespace PHPCR\Tests\Writing;
3+
4+
use Jackalope\Session;
5+
use PHPCR\PropertyInterface;
6+
7+
require_once(__DIR__ . '/../../inc/BaseCase.php');
8+
9+
/**
10+
* Testing the mix:lastModified support when the values are never updated
11+
* automatically.
12+
*/
13+
class LastModifiedTest extends \PHPCR\Test\BaseCase
14+
{
15+
public static function setupBeforeClass($fixtures = '10_Writing/lastmodified')
16+
{
17+
parent::setupBeforeClass($fixtures);
18+
}
19+
20+
public function setUp()
21+
{
22+
parent::setUp();
23+
24+
if (self::$loader->doesSessionLastModified()) {
25+
$this->markTestSkipped('Session updates lastModified automatically');
26+
}
27+
}
28+
29+
/**
30+
* Add mixin to an existing and to a new node.
31+
*/
32+
public function testCreate()
33+
{
34+
$this->assertFalse($this->node->hasProperty('jcr:lastModified'));
35+
$this->node->addMixin('mix:lastModified');
36+
37+
$this->session->save();
38+
39+
$this->assertTrue($this->node->hasProperty('jcr:lastModifiedBy'));
40+
$this->assertTrue($this->node->hasProperty('jcr:lastModified'));
41+
$this->assertSimilarDateTime(new \DateTime(), $this->node->getPropertyValue('jcr:lastModified'));
42+
43+
$node = $this->node->addNode('child');
44+
$node->addMixin('mix:lastModified');
45+
46+
$this->session->save();
47+
48+
$this->assertTrue($node->hasProperty('jcr:lastModifiedBy'));
49+
$this->assertTrue($node->hasProperty('jcr:lastModified'));
50+
$this->assertSimilarDateTime(new \DateTime(), $node->getPropertyValue('jcr:lastModified'));
51+
}
52+
53+
/**
54+
* When setting the lastModified information manually, it should not be
55+
* overwritten.
56+
*/
57+
public function testCreateManual()
58+
{
59+
$this->assertFalse($this->node->hasProperty('jcr:lastModified'));
60+
$this->node->addMixin('mix:lastModified');
61+
$this->node->setProperty('jcr:lastModifiedBy', 'me');
62+
63+
$this->session->save();
64+
65+
$this->assertTrue($this->node->hasProperty('jcr:lastModifiedBy'));
66+
$this->assertEquals('me', $this->node->getPropertyValue('jcr:lastModifiedBy'));
67+
$this->assertTrue($this->node->hasProperty('jcr:lastModified'));
68+
$this->assertSimilarDateTime(new \DateTime(), $this->node->getPropertyValue('jcr:lastModified'));
69+
70+
$node = $this->node->addNode('child');
71+
$node->addMixin('mix:lastModified');
72+
$date = new \DateTime('2012-01-02');
73+
$node->setProperty('jcr:lastModified', $date);
74+
75+
$this->session->save();
76+
77+
$this->assertTrue($node->hasProperty('jcr:lastModifiedBy'));
78+
$this->assertTrue($node->hasProperty('jcr:lastModified'));
79+
$this->assertEqualDateTime($date, $node->getPropertyValue('jcr:lastModified'));
80+
}
81+
82+
public function testUpdateText()
83+
{
84+
$date = $this->node->getPropertyValue('jcr:lastModified');
85+
$this->node->setProperty('text', 'new');
86+
87+
$this->session->save();
88+
89+
$this->assertEqualDateTime($date, $this->node->getPropertyValue('jcr:lastModified'));
90+
}
91+
92+
public function testUpdateManual()
93+
{
94+
$date = new \DateTime('2013-10-10');
95+
$this->node->setProperty('jcr:lastModified', $date);
96+
$this->node->setProperty('text', 'new');
97+
98+
$this->session->save();
99+
100+
$this->assertEqualDateTime($date, $this->node->getPropertyValue('jcr:lastModified'));
101+
}
102+
103+
public function testUpdateBinary()
104+
{
105+
$date = $this->node->getPropertyValue('jcr:lastModified');
106+
$stream = fopen('php://memory', 'w+');
107+
fwrite($stream, 'foo bar');
108+
rewind($stream);
109+
$this->node->setProperty('binary-data', $stream);
110+
111+
$this->session->save();
112+
113+
$this->assertEqualDateTime($date, $this->node->getPropertyValue('jcr:lastModified'));
114+
}
115+
116+
public function testRemoveProperty()
117+
{
118+
$date = $this->node->getPropertyValue('jcr:lastModified');
119+
$this->node->setProperty('text', null);
120+
121+
$this->session->save();
122+
123+
$this->assertEqualDateTime($date, $this->node->getPropertyValue('jcr:lastModified'));
124+
}
125+
}

0 commit comments

Comments
 (0)