Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:microweber/microweber into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-mw committed Oct 28, 2021
2 parents 184625d + acce26d commit 0cbfdc2
Show file tree
Hide file tree
Showing 15 changed files with 348 additions and 19 deletions.
53 changes: 51 additions & 2 deletions src/MicroweberPackages/App/Http/Controllers/RssController.php
Expand Up @@ -21,9 +21,15 @@ public function index(Request $request)
$view = 'wordpress';
}

$lang = $request->get('lang', false);

$contentData = [];
if($request->lang && $this->isMutilangOn() && is_lang_supported($request->lang)) {
change_language_by_locale($request->lang,false);
if ($this->isMutilangOn()) {
if ($lang && is_lang_supported($lang)) {
change_language_by_locale($lang, false);
} else {
change_language_by_locale(app()->lang_helper->default_lang(), false);
}
}

$filter = '';
Expand Down Expand Up @@ -68,6 +74,49 @@ public function index(Request $request)
return response()->view('rss::'.$view, $data)->header('Content-Type', 'text/xml');
}

public function posts(Request $request)
{
$contentData = [];

if($request->lang && $this->isMutilangOn() && is_lang_supported($request->lang)) {
change_language_by_locale($request->lang,false);
}

$siteTitle = app()->option_manager->get('website_title', 'website');
$siteDesc = app()->option_manager->get('website_description', 'website');

$posts = get_content('is_active=1&is_deleted=0&subtype=post&limit=2500&orderby=updated_at desc');

if(!empty($posts)) {
foreach($posts as $post) {
$tmp = [];

$picture = get_picture($post['id']);
$priceData = get_product_prices($post['id'], false);
$price = !empty($priceData['price']) ? $priceData['price'] : null;

$tmp['title'] = $post['title'];
$tmp['description'] = $post['description'];
$tmp['url'] = content_link($post['id']);
$tmp['image'] = $picture;
$tmp['price'] = $price;

$contentData[] = $tmp;
}
}

$data = [
'siteTitle' => $siteTitle,
'siteDescription' => $siteDesc,
'siteUrl' => mw()->url_manager->hostname(),
'rssData' => $contentData,
];

return response()
->view('rss::posts', $data)
->header('Content-Type', 'text/xml');
}

public function products(Request $request)
{
$contentData = [];
Expand Down
Expand Up @@ -18,7 +18,7 @@
@php
if (!empty($item['categories'])) {
foreach ($item['categories'] as $catItem) {
echo '<category term="'.$catItem['title'].'"></category>';
echo '<category term="'.urlencode($catItem['title']).'"></category>';
}
}
@endphp
Expand Down
21 changes: 21 additions & 0 deletions src/MicroweberPackages/App/resources/views/rss/posts.blade.php
@@ -0,0 +1,21 @@
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<rss version="2.0"
xmlns:g="http://base.google.com/ns/1.0">
<channel>
<title>{{ $siteTitle }}</title>
<link>{{ $siteUrl }}</link>
<description>{{ $siteDescription }}</description>
@foreach ($rssData as $item)
<item>
<g:title>{{ $item['title'] }}</g:title>
<g:description>{{ $item['description'] }}</g:description>
@if(!empty($item['image']))
<g:image_link>{{ $item['image'] }}</g:image_link>
@endif
@if(isset($item['price']))
<g:price>{{ $item['price'] }}</g:price>
@endif
</item>
@endforeach
</channel>
</rss>
1 change: 1 addition & 0 deletions src/MicroweberPackages/App/routes/web.php
Expand Up @@ -211,6 +211,7 @@ function migrateLanguages()

Route::any('rss', 'RssController@index')->name('rss.index');
Route::any('rss-products', 'RssController@products')->name('rss.products');
Route::any('rss-posts', 'RssController@posts')->name('rss.posts');

Route::any('{all}', array('as' => 'all', 'uses' => 'FrontendController@index'))->where('all', '.*');

Expand Down
49 changes: 49 additions & 0 deletions src/MicroweberPackages/App/tests/RssControllerTest.php
Expand Up @@ -24,6 +24,55 @@ public function testIndex()
$this->assertTrue(is_object($rssXml));
}

public function testIndexWordpressFormat()
{
$tag = new Post();
$tag->title = 'title-'.str_random();
$tag->content = 'content-'.str_random();
$tag->save();

$response = $this->call('GET', route('rss.index'),['format'=>'wordpress']);
$this->assertEquals(200, $response->status());

$rssXmlContent = $response->getContent();

$rssXml = simplexml_load_string($rssXmlContent);
$this->assertTrue(is_object($rssXml));
}

public function testIndexWithParent()
{
$tag = new Post();
$tag->title = 'title-'.str_random();
$tag->content = 'content-'.str_random();
$tag->save();

$response = $this->call('GET', route('rss.index'),['parent_id'=>1]);
$this->assertEquals(200, $response->status());

$rssXmlContent = $response->getContent();

$rssXml = simplexml_load_string($rssXmlContent);
$this->assertTrue(is_object($rssXml));
}

public function testPosts()
{
$tag = new Post();
$tag->title = 'title-'.str_random();
$tag->content = 'content-'.str_random();
$tag->save();

$response = $this->call('GET', route('rss.posts'),[]);
$this->assertEquals(200, $response->status());

$rssXmlContent = $response->getContent();

$rssXml = simplexml_load_string($rssXmlContent);
$this->assertTrue(is_object($rssXml));

}

public function testProducts()
{
$tag = new Product();
Expand Down
Expand Up @@ -113,9 +113,7 @@ public function delete($id)
*/
public function destroy($ids)
{
$result = $this->category->destroy($ids);
return true;
// return (new JsonResource($result))->response();
return (new JsonResource(['ids'=>$this->category->destroy($ids)]));
}

}
Expand Up @@ -84,7 +84,7 @@ public function delete($id)
*/
public function destroy($ids)
{
return (new JsonResource($this->content->destroy($ids)));
return (new JsonResource(['ids'=>$this->content->destroy($ids)]));
}


Expand Down
211 changes: 211 additions & 0 deletions src/MicroweberPackages/Content/tests/ContentApiControllerTest.php
@@ -0,0 +1,211 @@
<?php
namespace MicroweberPackages\Content\tests;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use MicroweberPackages\Category\Models\Category;
use MicroweberPackages\Core\tests\TestCase;
use MicroweberPackages\User\Models\User;

class ContentApiControllerTest extends TestCase
{
public function testAddContentFull()
{
$categoryIds = [];

$user = User::where('is_admin','=', '1')->first();
Auth::login($user);


$category = new Category();
$category->title = 'New cat for my custom model'. rand();
$category->save();
$categoryIds[] = $category->id;

$category = new Category();
$category->title = 'New cat for my custom model'. rand();
$category->save();
$categoryIds[] = $category->id;

$title = 'Iphone and spire 4ever! - '. rand();
$title2 = 'Iphone and spire 4ever! 2 - '. rand();
$contentBody = 'This is my cool content description.';


$response = $this->call(
'POST',
route('api.content.store'),
[
'title' => $title,
'category_ids'=>implode(',', $categoryIds),
'content_body' => $contentBody,
'content' => '',
]
);

$contentDataSaved = $response->getData()->data;
$this->assertEquals($contentDataSaved->title, $title);



$response = $this->call(
'PUT',
route('api.content.update', [
'content' => $contentDataSaved->id,
'title' => $title2,
])

);

$this->assertEquals(200, $response->status());
$contentDataSaved = $response->getData()->data;

$this->assertEquals($contentDataSaved->title, $title2);


$response = $this->call(
'PUT',
route('api.content.update', [
'content' => $contentDataSaved->id,
'title' => 'new title',
])

);
$this->assertEquals(200, $response->status());

$contentDataSaved = $response->getData()->data;
$this->assertEquals($contentDataSaved->title, 'new title');


$response = $this->call(
'PUT',
route('api.content.update', [
'content' => $contentDataSaved->id,
'title' => '0',
])

);
$this->assertEquals(200, $response->status());

$contentDataSaved = $response->getData()->data;
$this->assertEquals($contentDataSaved->title, 0);


$response = $this->call(
'PUT',
route('api.content.update', [
'content' => $contentDataSaved->id,
])

);
$this->assertEquals(200, $response->status());

$contentDataSaved = $response->getData()->data;
}

public function testSaveContentFromController()
{
$user = User::where('is_admin','=', '1')->first();
Auth::login($user);

$title = 'Test add content from api ' . rand();
$title2 = 'Test update content from api ' . rand();

$response = $this->call(
'POST',
route('api.content.store'),
[
'title' => $title,
'content_body' => '<b>Bold text</b>',
'content' => '<b onmouseover=alert(‘XSS testing!‘)>XSS</b> <IMG SRC=j&#X41vascript:alert(\'test2\')>'
]
);


$this->assertEquals(201, $response->status());
$contentData = $response->getData();
$this->assertEquals($contentData->data->title, $title);

$content_id = $contentData->data->id;


$response = $this->call(
'GET',
route('api.content.show',
[
'content' => $content_id,
])
);

$contentData = $response->getData();


$this->assertEquals($contentData->data->title, $title);


$response = $this->call(
'PUT',
route('api.content.update', [
'content' => $content_id,
'title' => $title2,
])

);

$this->assertEquals(200, $response->status());

$response = $this->call(
'GET',
route('api.content.show',
[
'content' => $content_id,
])
);

$contentData = $response->getData();

$this->assertEquals($contentData->data->title, $title2);



$response = $this->call(
'GET',
route('api.content.index',
[
])
);

$contentData = $response->getData();
$this->assertEquals(true,!empty($contentData->data));

}

public function testDeleteContentFromController()
{
$user = User::where('is_admin', '=', '1')->first();
Auth::login($user);

$title = 'Test add content from api ' . rand();

$response = $this->call(
'POST',
route('api.content.store'),
[
'title' => $title,
]
);


$response = $this->call(
'DELETE',
route('api.content.destroy', [
'content' => $response->getData()->data->id,
])
);

$this->assertEquals(200, $response->status());
$contentData = $response->getData()->data->ids;

$this->assertNotEmpty($contentData);
}
}

0 comments on commit 0cbfdc2

Please sign in to comment.