From 4518d2a524e2a440dc5acedae15e4fad2a114aea Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Sun, 5 Jan 2014 10:29:49 +0100 Subject: [PATCH] add tests for mix:lastModified and handling for implementation loader --- fixtures/10_Writing/lastmodified.xml | 91 ++++++++++++++ inc/AbstractLoader.php | 33 ++++++ inc/BaseCase.php | 17 +++ tests/10_Writing/LastModifiedTest.php | 125 ++++++++++++++++++++ tests/10_Writing/LastModifiedUpdateTest.php | 120 +++++++++++++++++++ 5 files changed, 386 insertions(+) create mode 100644 fixtures/10_Writing/lastmodified.xml create mode 100644 tests/10_Writing/LastModifiedTest.php create mode 100644 tests/10_Writing/LastModifiedUpdateTest.php diff --git a/fixtures/10_Writing/lastmodified.xml b/fixtures/10_Writing/lastmodified.xml new file mode 100644 index 00000000..26e2b965 --- /dev/null +++ b/fixtures/10_Writing/lastmodified.xml @@ -0,0 +1,91 @@ + + + + nt:unstructured + + + 2009-04-27T13:00:54.082+02:00 + + + + nt:unstructured + + + + + nt:unstructured + + + + + nt:unstructured + + + mix:lastModified + + + 2011-05-20T08:20:30.108+02:00 + + + _phpcr-api-tests + + + old + + + + + nt:unstructured + + + mix:lastModified + + + 2011-05-20T08:20:30.108+02:00 + + + _phpcr-api-tests + + + old + + + + + nt:unstructured + + + mix:lastModified + + + 2011-05-20T08:20:30.108+02:00 + + + _phpcr-api-tests + + + aDEuIENoYXB0ZXIgMSBUaXRsZQoKKiBmb28KKiBiYXIKKiogZm9vMgoqKiBmb28zCiogZm9vMAoKfHwgaGVhZGVyIHx8IGJhciB8fAp8IGggfCBqIHwKCntjb2RlfQpoZWxsbyB3b3JsZAp7Y29kZX0KCiMgZm9vCg== + + + + + nt:unstructured + + + mix:lastModified + + + 2011-05-20T08:20:30.108+02:00 + + + _phpcr-api-tests + + + old + + + diff --git a/inc/AbstractLoader.php b/inc/AbstractLoader.php index d6d80d2d..7be69a62 100644 --- a/inc/AbstractLoader.php +++ b/inc/AbstractLoader.php @@ -176,6 +176,39 @@ public function getAdditionalSession($credentials = false) return $this->getSessionForWorkspace($credentials, $this->getOtherWorkspaceName()); } + /** + * If the implementation can automatically update mix:lastModified nodes, + * this should return a session configured to do that. + * + * Otherwise, the test regarding this feature is skipped. + * + * @return \PHPCR\SessionInterface + * + * @throws \PHPUnit_Framework_SkippedTestSuiteError to make whole test + * suite skip if implementation does not support updating the + * properties automatically. + */ + public function getSessionWithLastModified() + { + if ($this->doesSessionLastModified()) { + return $this->getSession(); + } + + throw new \PHPUnit_Framework_SkippedTestSuiteError('Not supported'); + } + + /** + * The implementation loader should provide a session that does not update + * mix:lastModified. If that is not possible, this method should return + * true, which will skip the test about this feature. + * + * @return boolean + */ + public function doesSessionLastModified() + { + return false; + } + /** * Decide whether this test can be executed. * diff --git a/inc/BaseCase.php b/inc/BaseCase.php index fdfd8b3d..8ab86a9d 100644 --- a/inc/BaseCase.php +++ b/inc/BaseCase.php @@ -252,4 +252,21 @@ protected function assertEqualDateTime(DateTime $date1, DateTime $date2) { $this->assertEquals($date1->getTimestamp(), $date2->getTimestamp()); } + + /** + * Assert that both arguments are datetime and are within 3 seconds of each + * other. Use this rather than plain "Equal" when checking application + * generated dates. + * + * @param \DateTime $expected + * @param \DateTime $data + */ + protected function assertSimilarDateTime($expected, $data) + { + $this->assertInstanceOf('\DateTime', $expected); + $this->assertInstanceOf('\DateTime', $data); + $this->assertTrue(abs($expected->getTimestamp() - $data->getTimestamp()) <= 3, + $data->format('c') . ' is not close to the expected ' . $expected->format('c') + ); + } } diff --git a/tests/10_Writing/LastModifiedTest.php b/tests/10_Writing/LastModifiedTest.php new file mode 100644 index 00000000..f66fcbed --- /dev/null +++ b/tests/10_Writing/LastModifiedTest.php @@ -0,0 +1,125 @@ +doesSessionLastModified()) { + $this->markTestSkipped('Session updates lastModified automatically'); + } + } + + /** + * Add mixin to an existing and to a new node. + */ + public function testCreate() + { + $this->assertFalse($this->node->hasProperty('jcr:lastModified')); + $this->node->addMixin('mix:lastModified'); + + $this->session->save(); + + $this->assertTrue($this->node->hasProperty('jcr:lastModifiedBy')); + $this->assertTrue($this->node->hasProperty('jcr:lastModified')); + $this->assertSimilarDateTime(new \DateTime(), $this->node->getPropertyValue('jcr:lastModified')); + + $node = $this->node->addNode('child'); + $node->addMixin('mix:lastModified'); + + $this->session->save(); + + $this->assertTrue($node->hasProperty('jcr:lastModifiedBy')); + $this->assertTrue($node->hasProperty('jcr:lastModified')); + $this->assertSimilarDateTime(new \DateTime(), $node->getPropertyValue('jcr:lastModified')); + } + + /** + * When setting the lastModified information manually, it should not be + * overwritten. + */ + public function testCreateManual() + { + $this->assertFalse($this->node->hasProperty('jcr:lastModified')); + $this->node->addMixin('mix:lastModified'); + $this->node->setProperty('jcr:lastModifiedBy', 'me'); + + $this->session->save(); + + $this->assertTrue($this->node->hasProperty('jcr:lastModifiedBy')); + $this->assertEquals('me', $this->node->getPropertyValue('jcr:lastModifiedBy')); + $this->assertTrue($this->node->hasProperty('jcr:lastModified')); + $this->assertSimilarDateTime(new \DateTime(), $this->node->getPropertyValue('jcr:lastModified')); + + $node = $this->node->addNode('child'); + $node->addMixin('mix:lastModified'); + $date = new \DateTime('2012-01-02'); + $node->setProperty('jcr:lastModified', $date); + + $this->session->save(); + + $this->assertTrue($node->hasProperty('jcr:lastModifiedBy')); + $this->assertTrue($node->hasProperty('jcr:lastModified')); + $this->assertEqualDateTime($date, $node->getPropertyValue('jcr:lastModified')); + } + + public function testUpdateText() + { + $date = $this->node->getPropertyValue('jcr:lastModified'); + $this->node->setProperty('text', 'new'); + + $this->session->save(); + + $this->assertEqualDateTime($date, $this->node->getPropertyValue('jcr:lastModified')); + } + + public function testUpdateManual() + { + $date = new \DateTime('2013-10-10'); + $this->node->setProperty('jcr:lastModified', $date); + $this->node->setProperty('text', 'new'); + + $this->session->save(); + + $this->assertEqualDateTime($date, $this->node->getPropertyValue('jcr:lastModified')); + } + + public function testUpdateBinary() + { + $date = $this->node->getPropertyValue('jcr:lastModified'); + $stream = fopen('php://memory', 'w+'); + fwrite($stream, 'foo bar'); + rewind($stream); + $this->node->setProperty('binary-data', $stream); + + $this->session->save(); + + $this->assertEqualDateTime($date, $this->node->getPropertyValue('jcr:lastModified')); + } + + public function testRemoveProperty() + { + $date = $this->node->getPropertyValue('jcr:lastModified'); + $this->node->setProperty('text', null); + + $this->session->save(); + + $this->assertEqualDateTime($date, $this->node->getPropertyValue('jcr:lastModified')); + } +} diff --git a/tests/10_Writing/LastModifiedUpdateTest.php b/tests/10_Writing/LastModifiedUpdateTest.php new file mode 100644 index 00000000..00499071 --- /dev/null +++ b/tests/10_Writing/LastModifiedUpdateTest.php @@ -0,0 +1,120 @@ +getSessionWithLastModified(); + } + + /** + * Add mixin to an existing and to a new node. + */ + public function testCreate() + { + $this->assertFalse($this->node->hasProperty('jcr:lastModified')); + $this->node->addMixin('mix:lastModified'); + + $this->session->save(); + + $this->assertTrue($this->node->hasProperty('jcr:lastModifiedBy')); + $this->assertTrue($this->node->hasProperty('jcr:lastModified')); + $this->assertSimilarDateTime(new \DateTime(), $this->node->getPropertyValue('jcr:lastModified')); + + $node = $this->node->addNode('child'); + $node->addMixin('mix:lastModified'); + + $this->session->save(); + + $this->assertTrue($node->hasProperty('jcr:lastModifiedBy')); + $this->assertTrue($node->hasProperty('jcr:lastModified')); + $this->assertSimilarDateTime(new \DateTime(), $node->getPropertyValue('jcr:lastModified')); + } + + /** + * When setting the lastModified information manually, it should not be + * overwritten. + */ + public function testCreateManual() + { + $this->assertFalse($this->node->hasProperty('jcr:lastModified')); + $this->node->addMixin('mix:lastModified'); + $this->node->setProperty('jcr:lastModifiedBy', 'me'); + + $this->session->save(); + + $this->assertTrue($this->node->hasProperty('jcr:lastModifiedBy')); + $this->assertEquals('me', $this->node->getPropertyValue('jcr:lastModifiedBy')); + $this->assertTrue($this->node->hasProperty('jcr:lastModified')); + $this->assertSimilarDateTime(new \DateTime(), $this->node->getPropertyValue('jcr:lastModified')); + + $node = $this->node->addNode('child'); + $node->addMixin('mix:lastModified'); + $date = new \DateTime('2012-01-02'); + $node->setProperty('jcr:lastModified', $date); + + $this->session->save(); + + $this->assertTrue($node->hasProperty('jcr:lastModifiedBy')); + $this->assertTrue($node->hasProperty('jcr:lastModified')); + $this->assertEqualDateTime($date, $node->getPropertyValue('jcr:lastModified')); + } + + public function testUpdateText() + { + $this->node->setProperty('text', 'new'); + + $this->session->save(); + + $this->assertSimilarDateTime(new \DateTime(), $this->node->getPropertyValue('jcr:lastModified')); + } + + public function testUpdateManual() + { + $date = new \DateTime('2013-10-10'); + $this->node->setProperty('jcr:lastModified', $date); + $this->node->setProperty('text', 'new'); + + $this->session->save(); + + $this->assertEqualDateTime($date, $this->node->getPropertyValue('jcr:lastModified')); + } + + public function testUpdateBinary() + { + $stream = fopen('php://memory', 'w+'); + fwrite($stream, 'foo bar'); + rewind($stream); + $this->node->setProperty('binary-data', $stream); + + $this->session->save(); + + $this->assertSimilarDateTime(new \DateTime(), $this->node->getPropertyValue('jcr:lastModified')); + } + + public function testRemoveProperty() + { + $this->node->setProperty('text', null); + + $this->session->save(); + + $this->assertSimilarDateTime(new \DateTime(), $this->node->getPropertyValue('jcr:lastModified')); + } +}