|
| 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