public function testSitemapModel() { Yii::$app->cache->flush(); $sitemap = new Sitemap(['models' => ['assayerpro\\sitemap\\tests\\unit\\Article', ['class' => 'assayerpro\\sitemap\\tests\\unit\\Gallery', 'behaviors' => ['sitemap' => ['class' => 'assayerpro\\sitemap\\behaviors\\SitemapBehavior', 'dataClosure' => function ($model) { /** @var \yii\db\ActiveQuery $model */ return ['loc' => $model->url, 'changefreq' => \assayerpro\sitemap\Sitemap::WEEKLY, 'priority' => 0.8]; }]]]]]); $expectedXML = <<<EOF <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"><url><loc>http://www.example.com/article/1-article-one</loc><changefreq>weekly</changefreq><priority>0.8</priority></url><url><loc>http://www.example.com/article/2-article-two</loc><changefreq>weekly</changefreq><priority>0.8</priority></url><url><loc>http://www.example.com/article/3-article-with-long-long-long-title</loc><changefreq>weekly</changefreq><priority>0.8</priority></url><url><loc>http://www.example.com/gallery/1-first-gallery</loc><changefreq>weekly</changefreq><priority>0.8</priority></url><url><loc>http://www.example.com/gallery/2-landscape</loc><changefreq>weekly</changefreq><priority>0.8</priority></url></urlset> EOF; $this->assertEquals($expectedXML, $sitemap->render()[0]['xml']); }
/** * Generate url's array from properties $url and $models * * @access protected * @return array */ protected function generateUrls() { $urls = $this->urls; foreach ($this->models as $modelName) { /** @var behaviors\SitemapBehavior $model */ if (is_array($modelName)) { $model = new $modelName['class'](); if (isset($modelName['behaviors'])) { $model->attachBehaviors($modelName['behaviors']); } } else { $model = new $modelName(); } $urls = array_merge($urls, $model->generateSiteMap()); } $urls = array_map(function ($item) { $item['loc'] = Url::to($item['loc'], true); if (isset($item['lastmod'])) { $item['lastmod'] = Sitemap::dateToW3C($item['lastmod']); } if (isset($item['images'])) { $item['images'] = array_map(function ($image) { $image['loc'] = Url::to($image['loc'], true); return $image; }, $item['images']); } return $item; }, $urls); return $urls; }