저자: Victor Puertas (vpgugr@gmail.com)
상속: implements Yosymfony\Spress\Core\DataSource\ItemInterface
예제 #1
0
 public function testsetRelativeUrl()
 {
     $item = new Item('Test of content', 'index.html', ['title' => 'My posts']);
     $item->setPath('index.html', Item::SNAPSHOT_PATH_RELATIVE);
     $event = new RenderEvent($item, Item::SNAPSHOT_RAW, Item::SNAPSHOT_PATH_RELATIVE);
     $event->setRelativeUrl('/welcome/index.html');
     $this->assertEquals('/welcome/index.html', $event->getRelativeUrl());
 }
 public function testWriteItem()
 {
     $item = new Item('Test content', 'my-id');
     $item->setPath('index.html', Item::SNAPSHOT_PATH_RELATIVE_AFTER_CONVERT);
     $item->setPath('index.html', Item::SNAPSHOT_PATH_PERMALINK);
     $dw = new FilesystemDataWriter(new Filesystem(), $this->tmpDir);
     $dw->write($item);
     $this->assertFileExists($this->tmpDir . '/index.html');
 }
예제 #3
0
 public function onStart(EnvironmentEvent $event)
 {
     $dsm = $event->getDataSourceManager();
     $item = new Item('Content from item in memory data source', 'memory-datasource.txt');
     $item->setPath('memory-datasource.txt', Item::SNAPSHOT_PATH_RELATIVE);
     $memoryDataSource = new MemoryDataSource();
     $memoryDataSource->addItem($item);
     $dsm->setDataSource('memory-plugin', $memoryDataSource);
 }
예제 #4
0
 public function testDefaultCollection()
 {
     $cm = new CollectionManager();
     $cm->addCollection(new Collection('events', '_events', ['output' => true]));
     $item = new Item('Test of content', 'member-1.html', []);
     $item->setPath('member-1.html', Item::SNAPSHOT_PATH_RELATIVE);
     $collection = $cm->getCollectionForItem($item);
     $this->assertEquals('pages', $collection->getName());
     $this->assertEquals('', $collection->getPath());
 }
예제 #5
0
 public function testConvertItemNoTextExtension()
 {
     $cm = new ConverterManager(['html']);
     $cm->addConverter(new MapConverter());
     $item = new Item('text', 'file.unknow');
     $item->setPath('file.unknow', Item::SNAPSHOT_PATH_RELATIVE);
     $result = $cm->convertItem($item);
     $this->assertEquals('text', $result->getResult());
     $this->assertEquals('unknow', $result->getExtension());
 }
예제 #6
0
 public function testDefaultCollection()
 {
     $cm = new CollectionManager();
     $this->assertCount(1, $cm->getCollectionItemCollection());
     $item = new Item('Test of content', 'member-1.html');
     $item->setPath('member-1.html', Item::SNAPSHOT_PATH_RELATIVE);
     $collection = $cm->getCollectionForItem($item);
     $this->assertEquals('pages', $collection->getName());
     $this->assertEquals('', $collection->getPath());
 }
예제 #7
0
 public function testWriteItem()
 {
     $item = new Item('Test content', 'my-id');
     $item->setPath('index.html', Item::SNAPSHOT_PATH_PERMALINK);
     $item->setPath('index.html', Item::SNAPSHOT_PATH_RELATIVE);
     $dw = new MemoryDataWriter();
     $dw->write($item);
     $this->assertTrue($dw->hasItem('index.html'));
     $this->assertEquals(1, $dw->countItems());
     $this->assertEquals('Test content', $dw->getItem('index.html')->getContent());
     $this->assertCount(1, $dw->getItems());
     $this->assertFalse($dw->hasItem('not-found-path.html'));
 }
예제 #8
0
 public function testContentEvent()
 {
     $item = new Item('Test of content', 'index.html', ['title' => 'My posts']);
     $item->setPath('index.html', Item::SNAPSHOT_PATH_RELATIVE);
     $event = new ContentEvent($item, Item::SNAPSHOT_RAW, Item::SNAPSHOT_PATH_RELATIVE);
     $this->assertEquals('index.html', $event->getId());
     $this->assertEquals('Test of content', $event->getContent());
     $this->assertTrue(is_array($event->getAttributes()));
     $this->assertCount(1, $event->getAttributes());
     $this->assertArrayHasKey('title', $event->getAttributes());
     $this->assertInstanceOf('\\Yosymfony\\Spress\\Core\\DataSource\\ItemInterface', $event->getItem());
     $attributes = $event->getAttributes();
     $attributes['name'] = 'Yo! Symtony';
     $event->setAttributes($attributes);
     $this->assertCount(2, $event->getAttributes());
     $this->assertArrayHasKey('name', $event->getAttributes());
     $event->setContent('New content');
     $this->assertEquals('New content', $event->getContent());
     $this->assertEquals('New content', $event->getItem()->getContent(Item::SNAPSHOT_RAW));
 }
예제 #9
0
 /**
  * @inheritDoc
  */
 public function getAttributes()
 {
     $attributes = parent::getAttributes();
     if (isset($attributes['pagination']) === false) {
         return $attributes;
     }
     $attributes['pagination']['items'] = [];
     foreach ($this->pageItems as $item) {
         $attributes['pagination']['items'][$item->getId()] = $this->getItemAttributes($item);
     }
     return $attributes;
 }
예제 #10
0
 public function testTaxonomyWithDefaulValues()
 {
     $post1 = new Item('Post 1', 'posts/2015-05-26-new-release', ['categories' => ['news', 'releases']]);
     $post2 = new Item('Post 2', 'posts/2015-05-26-new-feature', ['categories' => ['news', '']]);
     $collections = ['posts' => [$post1, $post2]];
     $templateItem = new Item('Categories content', 'categories/index.html', ['max_page' => 1]);
     $templateItem->setPath('categories/index.html', Item::SNAPSHOT_PATH_RELATIVE);
     $catetoriesItems = $this->taxonomy->generateItems($templateItem, $collections);
     $this->assertTrue(is_array($catetoriesItems));
     $this->assertCount(3, $catetoriesItems);
     $this->assertContainsOnly('\\Yosymfony\\Spress\\Core\\DataSource\\ItemInterface', $catetoriesItems);
     $item = $catetoriesItems[0];
     $this->assertArrayHasKey('pagination', $item->getAttributes());
     $this->assertArrayHasKey('term', $item->getAttributes());
     $this->assertEquals('news', $item->getAttributes()['term']);
     $this->assertEquals('categories/news/index.html', $item->getPath(Item::SNAPSHOT_PATH_RELATIVE));
     $this->assertEquals('/categories/news', $item->getPath(Item::SNAPSHOT_PATH_PERMALINK));
     $this->assertEquals('Categories content', $item->getContent());
     $item = $catetoriesItems[1];
     $this->assertArrayHasKey('pagination', $item->getAttributes());
     $this->assertArrayHasKey('term', $item->getAttributes());
     $this->assertEquals('news', $item->getAttributes()['term']);
     $this->assertEquals('categories/news/page2/index.html', $item->getPath(Item::SNAPSHOT_PATH_RELATIVE));
     $this->assertEquals('/categories/news/page2', $item->getPath(Item::SNAPSHOT_PATH_PERMALINK));
     $this->assertEquals('Categories content', $item->getContent());
     $item = $catetoriesItems[2];
     $this->assertArrayHasKey('pagination', $item->getAttributes());
     $this->assertArrayHasKey('term', $item->getAttributes());
     $this->assertEquals('releases', $item->getAttributes()['term']);
     $this->assertEquals('categories/releases/index.html', $item->getPath(Item::SNAPSHOT_PATH_RELATIVE));
     $this->assertEquals('/categories/releases', $item->getPath(Item::SNAPSHOT_PATH_PERMALINK));
     $this->assertEquals('Categories content', $item->getContent());
     $attributes = $post1->getAttributes();
     $this->assertArrayHasKey('terms_url', $attributes);
     $this->assertArrayHasKey('categories', $attributes['terms_url']);
     $this->assertCount(2, $attributes['terms_url']['categories']);
     $this->assertEquals('/categories/news', $attributes['terms_url']['categories']['news']);
     $this->assertEquals('/categories/releases', $attributes['terms_url']['categories']['releases']);
 }
예제 #11
0
 public function testTaxonomiesPointingToSameSlugedTerm()
 {
     $post1 = new Item('Post 1', 'posts/2015-05-26-new-release', ['categories' => ['баш']]);
     $post2 = new Item('Post 2', 'posts/2015-05-27-new-feature', ['categories' => ['баШ']]);
     $post3 = new Item('Post 3', 'posts/2015-05-28-new-feature', ['categories' => ['bash']]);
     $collections = ['posts' => [$post1, $post2, $post3]];
     $templateItem = new Item('Categories content', 'categories/index.html', ['max_page' => 3]);
     $templateItem->setPath('categories/index.html', Item::SNAPSHOT_PATH_RELATIVE);
     $catetoriesItems = $this->taxonomy->generateItems($templateItem, $collections);
     $this->assertTrue(is_array($catetoriesItems));
     $this->assertCount(1, $catetoriesItems);
     $item = $catetoriesItems[0];
     $this->assertArrayHasKey('pagination', $item->getAttributes());
     $this->assertArrayHasKey('term', $item->getAttributes());
     $this->assertEquals('баш', $item->getAttributes()['term']);
     $this->assertEquals('categories/bash/index.html', $item->getPath(Item::SNAPSHOT_PATH_RELATIVE));
     $attributes = $post1->getAttributes();
     $this->assertArrayHasKey('terms_url', $attributes);
     $this->assertArrayHasKey('categories', $attributes['terms_url']);
     $this->assertCount(2, $attributes['terms_url']['categories']);
     $this->assertEquals('/categories/bash', $attributes['terms_url']['categories']['bash']);
     $this->assertEquals('/categories/bash', $attributes['terms_url']['categories']['баш']);
 }
예제 #12
0
 private function createItem($path, $attributes = [], $binary = false)
 {
     $item = new Item('', $path, $attributes, $binary);
     $item->setPath($path, Item::SNAPSHOT_PATH_RELATIVE);
     return $item;
 }
예제 #13
0
 public function testSetItemPostCollection()
 {
     $site = new SiteAttribute();
     $site->initialize();
     $item = new Item('The content', 'posts/2015-06-22-hi.md', ['title' => 'Welcome', 'categories' => ['news'], 'tags' => ['release']]);
     $item->setCollection('posts');
     $item->setPath('2015/06/22/welcome/index.html', Item::SNAPSHOT_PATH_RELATIVE);
     $site->setItem($item);
     $arr = $site->getAttributes();
     $this->assertTrue(is_array($arr));
     $this->assertArrayHasKey('page', $arr);
     $this->assertArrayHasKey('categories', $arr['page']);
     $this->assertArrayHasKey('tags', $arr['page']);
     $this->assertArrayHasKey('posts', $arr['site']);
     $this->assertArrayHasKey('news', $arr['site']['categories']);
     $this->assertArrayHasKey('release', $arr['site']['tags']);
     $this->assertArrayHasKey('posts/2015-06-22-hi.md', $arr['site']['posts']);
     $this->assertArrayHasKey('posts/2015-06-22-hi.md', $arr['site']['categories']['news']);
     $this->assertArrayHasKey('posts/2015-06-22-hi.md', $arr['site']['tags']['release']);
     $this->assertArrayHasKey('collections', $arr['site']);
     $this->assertArrayHasKey('categories', $arr['site']);
     $this->assertArrayHasKey('tags', $arr['site']);
     $this->assertArrayHasKey('collection', $arr['page']);
     $this->assertArrayHasKey('title', $arr['page']);
     $this->assertArrayHasKey('id', $arr['page']);
     $this->assertArrayHasKey('path', $arr['page']);
     $this->assertCount(1, $arr['site']['posts']);
     $this->assertCount(0, $arr['site']['collections']);
     $this->assertCount(1, $arr['site']['categories']);
     $this->assertCount(1, $arr['site']['tags']);
     $this->assertEquals('posts', $arr['site']['posts']['posts/2015-06-22-hi.md']['collection']);
     $this->assertEquals('Welcome', $arr['site']['posts']['posts/2015-06-22-hi.md']['title']);
     $this->assertEquals('posts/2015-06-22-hi.md', $arr['site']['posts']['posts/2015-06-22-hi.md']['id']);
     $this->assertEquals('2015/06/22/welcome/index.html', $arr['site']['posts']['posts/2015-06-22-hi.md']['path']);
     $this->assertEquals('posts', $arr['site']['categories']['news']['posts/2015-06-22-hi.md']['collection']);
     $this->assertEquals('Welcome', $arr['site']['categories']['news']['posts/2015-06-22-hi.md']['title']);
     $this->assertEquals('posts/2015-06-22-hi.md', $arr['site']['categories']['news']['posts/2015-06-22-hi.md']['id']);
     $this->assertEquals('2015/06/22/welcome/index.html', $arr['site']['categories']['news']['posts/2015-06-22-hi.md']['path']);
     $this->assertEquals('posts', $arr['site']['tags']['release']['posts/2015-06-22-hi.md']['collection']);
     $this->assertEquals('Welcome', $arr['site']['tags']['release']['posts/2015-06-22-hi.md']['title']);
     $this->assertEquals('posts/2015-06-22-hi.md', $arr['site']['tags']['release']['posts/2015-06-22-hi.md']['id']);
     $this->assertEquals('2015/06/22/welcome/index.html', $arr['site']['tags']['release']['posts/2015-06-22-hi.md']['path']);
     $this->assertEquals('posts', $arr['page']['collection']);
     $this->assertEquals('Welcome', $arr['page']['title']);
     $this->assertEquals('posts/2015-06-22-hi.md', $arr['page']['id']);
     $this->assertEquals('2015/06/22/welcome/index.html', $arr['page']['path']);
     $this->assertCount(1, $arr['page']['categories']);
     $this->assertCount(1, $arr['page']['tags']);
 }
예제 #14
0
 /**
  * @expectedException \RuntimeException
  * @expectedExceptionMessage The item with id: "post1.md" has been registered previously with another collection.
  */
 public function testRegisterItemInSeveralCollections()
 {
     $collection = new ItemCollection();
     $item1 = new Item('', 'post1.md', ['date' => '2016-01-20']);
     $item1->setCollection('posts');
     $collection->set($item1);
     $item1->setCollection('events');
     $collection->set($item1);
 }
예제 #15
0
 /**
  * @expectedException \RuntimeException
  */
 public function testNullCollection()
 {
     $item = new Item('Raw content', 'index.html', []);
     $item->setCollection(null);
 }
예제 #16
0
 private function processAttributes(Item $item, SplFileInfo $file)
 {
     $attributes = [];
     $attributesFile = $this->getAttributesFilename($file);
     $isItemType = $item->getType() === Item::TYPE_ITEM;
     if ($attributesFile && file_exists($attributesFile)) {
         $contentFile = file_get_contents($attributesFile);
         $attributes = $this->attributeParser->getAttributesFromString($contentFile);
     } elseif (false === $item->isBinary()) {
         $attributes = $this->attributeParser->getAttributesFromFrontmatter($item->getContent());
         $content = $this->attributeParser->getContentFromFrontmatter($item->getContent());
         $item->setContent($content, Item::SNAPSHOT_RAW);
     }
     $fileInfo = new FileInfo($file->getPathname(), $this->params['text_extensions']);
     $avoidRender = $this->avoidRenderizer($fileInfo->getExtension(), $file->getRelativePath());
     if ($isItemType && $avoidRender === true && isset($attributes['avoid_renderizer']) === false) {
         $attributes['avoid_renderizer'] = true;
     }
     $attributes['mtime'] = $this->getModifiedTime($file);
     $attributes['filename'] = $fileInfo->getFilename();
     $attributes['extension'] = $fileInfo->getExtension();
     if ($data = $this->isDateFilename($attributes['filename'])) {
         $attributes['title_path'] = $data[3];
         if (isset($attributes['title']) === false) {
             $attributes['title'] = implode(' ', explode('-', $attributes['title_path']));
         }
         if (isset($attributes['date']) === false) {
             $attributes['date'] = implode('-', [$data[0], $data[1], $data[2]]);
         }
     }
     $str = new StringWrapper($this->normalizeDirSeparator($file->getRelativePath()));
     if ($isItemType && $str->startWith('posts/') === true && array_key_exists('categories', $attributes) === false) {
         $categories = explode('/', $str->deletePrefix('posts/'));
         if ($categories[0] === '') {
             unset($categories[0]);
         }
         $attributes['categories'] = $categories;
     }
     $item->setAttributes($attributes);
 }
예제 #17
0
 private function processAttributes(Item $item, SplFileInfo $file)
 {
     $attributes = [];
     $attributesFile = $this->getAttributesFilename($file);
     if ($attributesFile && file_exists($attributesFile)) {
         $contentFile = file_get_contents($attributesFile);
         $attributes = $this->attributeParser->getAttributesFromString($contentFile);
     } elseif (false === $item->isBinary()) {
         $attributes = $this->attributeParser->getAttributesFromFrontmatter($item->getContent());
         $content = $this->attributeParser->getContentFromFrontmatter($item->getContent());
         $item->setContent($content, Item::SNAPSHOT_RAW);
     }
     $attributes['mtime'] = $this->getModifiedTime($file);
     $attributes['filename'] = $file->getFilename();
     $attributes['extension'] = $file->getExtension();
     if ($data = $this->isDateFilename($file)) {
         $attributes['title_path'] = implode(' ', explode('-', $data[3]));
         if (isset($attributes['title']) === false) {
             $attributes['title'] = $attributes['title_path'];
         }
         if (isset($attributes['date']) === false) {
             $attributes['date'] = implode('-', [$data[0], $data[1], $data[2]]);
         }
     }
     $str = new StringWrapper($file->getRelativePath());
     if ($str->startWith('posts/') === true && array_key_exists('categories', $attributes) === false) {
         $categories = explode('/', $str->deletePrefix('posts/'));
         if ($categories[0] === '') {
             unset($categories[0]);
         }
         $attributes['categories'] = $categories;
     }
     $item->setAttributes($attributes);
 }
예제 #18
0
 public function testMultipleExtension()
 {
     $dw = new MemoryDataWriter();
     $dsm = $this->getMemoryDataSourceManager();
     $cm = $this->getContentManager($dsm, $dw);
     $item1 = new Item('', 'about.html.twig');
     $item1->setPath('about.html.twig', Item::SNAPSHOT_PATH_RELATIVE);
     $item2 = new Item('', 'docs/migrating-1.x-to-2.x.md');
     $item2->setPath('docs/migrating-1.x-to-2.x.md', Item::SNAPSHOT_PATH_RELATIVE);
     $memoryDataSource = $dsm->getDataSource('memory');
     $memoryDataSource->addItem($item1);
     $memoryDataSource->addItem($item2);
     $cm->parseSite([], [], true);
     $this->assertTrue($dw->hasItem('about/index.html'));
     $this->assertTrue($dw->hasItem('docs/migrating-1.x-to-2.x/index.html'));
 }
예제 #19
0
 public function testPaginateRootDir()
 {
     $post1 = new Item('Post 1', 'posts/2015-05-26-hi', []);
     $post2 = new Item('Post 2', 'posts/2015-05-26-welcome', []);
     $collections = ['posts' => [$post1, $post2]];
     $templateItem = new Item('Paginator content', 'index.html', ['max_page' => 1]);
     $templateItem->setPath('index.html', Item::SNAPSHOT_PATH_RELATIVE);
     $pageItems = $this->pagination->generateItems($templateItem, $collections);
     $this->assertTrue(is_array($pageItems));
     $this->assertCount(2, $pageItems);
     $page1 = $pageItems[0];
     $this->assertEquals('index.html', $page1->getPath(Item::SNAPSHOT_PATH_RELATIVE));
     $this->assertEquals('/', $page1->getPath(Item::SNAPSHOT_PATH_PERMALINK));
     $this->assertEquals('Paginator content', $page1->getContent());
     $attrPage1 = $page1->getAttributes();
     $this->assertArrayHasKey('pagination', $attrPage1);
     $this->assertArrayHasKey('items', $attrPage1['pagination']);
     $this->assertArrayHasKey('per_page', $attrPage1['pagination']);
     $this->assertArrayHasKey('total_items', $attrPage1['pagination']);
     $this->assertArrayHasKey('total_pages', $attrPage1['pagination']);
     $this->assertArrayHasKey('page', $attrPage1['pagination']);
     $this->assertArrayHasKey('previous_page', $attrPage1['pagination']);
     $this->assertArrayHasKey('previous_page_path', $attrPage1['pagination']);
     $this->assertArrayHasKey('previous_page_url', $attrPage1['pagination']);
     $this->assertArrayHasKey('next_page', $attrPage1['pagination']);
     $this->assertArrayHasKey('next_page_path', $attrPage1['pagination']);
     $this->assertArrayHasKey('next_page_url', $attrPage1['pagination']);
     $this->assertCount(1, $attrPage1['pagination']['items']);
     $this->assertEquals(1, $attrPage1['pagination']['per_page']);
     $this->assertEquals(2, $attrPage1['pagination']['total_items']);
     $this->assertEquals(2, $attrPage1['pagination']['total_pages']);
     $this->assertEquals(1, $attrPage1['pagination']['page']);
     $this->assertEquals(2, $attrPage1['pagination']['next_page']);
     $this->assertEquals('page2/index.html', $attrPage1['pagination']['next_page_path']);
     $this->assertEquals('/page2', $attrPage1['pagination']['next_page_url']);
     $this->assertNull($attrPage1['pagination']['previous_page']);
     $this->assertNull($attrPage1['pagination']['previous_page_path']);
     $this->assertNull($attrPage1['pagination']['previous_page_url']);
     $page2 = $pageItems[1];
     $this->assertEquals('page2/index.html', $page2->getPath(Item::SNAPSHOT_PATH_RELATIVE));
     $this->assertEquals('/page2', $page2->getPath(Item::SNAPSHOT_PATH_PERMALINK));
     $this->assertEquals('Paginator content', $page2->getContent());
     $attrPage2 = $page2->getAttributes();
     $this->assertArrayHasKey('pagination', $attrPage2);
     $this->assertArrayHasKey('items', $attrPage2['pagination']);
     $this->assertArrayHasKey('per_page', $attrPage2['pagination']);
     $this->assertArrayHasKey('total_items', $attrPage2['pagination']);
     $this->assertArrayHasKey('total_pages', $attrPage2['pagination']);
     $this->assertArrayHasKey('page', $attrPage2['pagination']);
     $this->assertArrayHasKey('previous_page', $attrPage2['pagination']);
     $this->assertArrayHasKey('previous_page_path', $attrPage2['pagination']);
     $this->assertArrayHasKey('previous_page_url', $attrPage2['pagination']);
     $this->assertArrayHasKey('next_page', $attrPage2['pagination']);
     $this->assertArrayHasKey('next_page_path', $attrPage2['pagination']);
     $this->assertArrayHasKey('next_page_url', $attrPage2['pagination']);
     $this->assertCount(1, $attrPage2['pagination']['items']);
     $this->assertEquals(1, $attrPage2['pagination']['per_page']);
     $this->assertEquals(2, $attrPage2['pagination']['total_items']);
     $this->assertEquals(2, $attrPage2['pagination']['total_pages']);
     $this->assertEquals(2, $attrPage2['pagination']['page']);
     $this->assertEquals(1, $attrPage2['pagination']['previous_page']);
     $this->assertEquals('index.html', $attrPage2['pagination']['previous_page_path']);
     $this->assertEquals('/', $attrPage2['pagination']['previous_page_url']);
     $this->assertNull($attrPage2['pagination']['next_page']);
     $this->assertNull($attrPage2['pagination']['next_page_path']);
     $this->assertNull($attrPage2['pagination']['next_page_url']);
 }