diff --git a/app/Http/Controllers/HomepageController.php b/app/Http/Controllers/HomepageController.php index b0dadbe3..153f2694 100644 --- a/app/Http/Controllers/HomepageController.php +++ b/app/Http/Controllers/HomepageController.php @@ -10,6 +10,7 @@ use Illuminate\View\View; use Illuminate\Http\Request; use Contracts\Repositories\HomepageRepositoryContract; +use Contracts\Repositories\ModularPageRepositoryContract; use Contracts\Repositories\EventRepositoryContract; use Contracts\Repositories\ArticleRepositoryContract; @@ -18,9 +19,14 @@ class HomepageController extends Controller /** * Construct the controller. */ - public function __construct(HomepageRepositoryContract $promo, ArticleRepositoryContract $article, EventRepositoryContract $event) - { + public function __construct( + HomepageRepositoryContract $promo, + ModularPageRepositoryContract $modularComponent, + ArticleRepositoryContract $article, + EventRepositoryContract $event + ) { $this->promo = $promo; + $this->modularComponent = $modularComponent; $this->article = $article; $this->event = $event; } @@ -31,12 +37,20 @@ public function __construct(HomepageRepositoryContract $promo, ArticleRepository public function index(Request $request): View { // $request->data['base']['show_site_menu'] = false; - // $promos = $this->promo->getHomepagePromos(); + + $promos = $this->promo->getHomepagePromos($request->data); + + $modularComponents['modularComponents'] = []; + + if(!empty($request->data['base']['data'])) { + $modularComponents['modularComponents'] = $this->modularComponent->getModularComponents($request->data['base']); + $promos['components'] = $modularComponents['modularComponents']; + } $articles = $this->article->listing($request->data['base']['site']['news']['application_id']); $events = $this->event->getEvents($request->data['base']['site']['id']); - return view('homepage', merge($request->data, $articles, $events)); + return view('homepage', merge($request->data, $promos, $articles, $events)); } } diff --git a/app/Repositories/HomepageRepository.php b/app/Repositories/HomepageRepository.php index 8d0010d7..aae70df4 100644 --- a/app/Repositories/HomepageRepository.php +++ b/app/Repositories/HomepageRepository.php @@ -31,14 +31,14 @@ public function __construct(Connector $wsuApi, ParsePromos $parsePromos, Reposit /** * {@inheritdoc} */ - public function getHomepagePromos(int $page_id = 0) + public function getHomepagePromos(array $data): array { $group_reference = [ 123 => 'example', ]; $group_config = [ - 'example' => 'page_id:'.$page_id.'|randomize|first', + 'example' => 'randomize|first', ]; $params = [ diff --git a/composer.lock b/composer.lock index 71ea8a41..90f842e2 100644 --- a/composer.lock +++ b/composer.lock @@ -9129,5 +9129,5 @@ "php": "^8.1" }, "platform-dev": [], - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.6.0" } diff --git a/contracts/Repositories/HomepageRepositoryContract.php b/contracts/Repositories/HomepageRepositoryContract.php index e6072547..949f013c 100644 --- a/contracts/Repositories/HomepageRepositoryContract.php +++ b/contracts/Repositories/HomepageRepositoryContract.php @@ -7,5 +7,5 @@ interface HomepageRepositoryContract /** * {@inheritdoc} */ - public function getHomepagePromos(int $page_id = 0); + public function getHomepagePromos(array $data): array; } diff --git a/factories/Catalog.php b/factories/Catalog.php new file mode 100644 index 00000000..1260ca0d --- /dev/null +++ b/factories/Catalog.php @@ -0,0 +1,53 @@ +faker = $faker->create(); + } + + /** + * {@inheritdoc} + */ + public function create($limit = 1, $flatten = false, $options = []) + { + // Doc https://github.com/fzaninotto/Faker + + $promo_group_id = $this->faker->randomNumber(5); + + $relative_url = '/styleguide/image/600x600?text=600x600'; // 1:1 + + + for ($i = 0; $i <= $limit - 1; $i++) { + $data[$i] = [ + 'title' => 'Call to action', + 'link' => '#', + 'promo_item_id' => $i, + 'promo_group_id' => strval($promo_group_id), + 'relative_url' => $relative_url.':'.$i, + 'filename_url' => $relative_url.':'.$i, + 'filename_alt_text' => 'Placeholder image '.$i, + 'group' => [ + 'title' => 'Modular Catalog Promotion Group', + ], + ]; + + $data[$i] = array_replace_recursive($data[$i], $options); + } + + if ($limit === 1 && $flatten === true) { + return current($data); + } + + return $data; + } +} diff --git a/resources/views/components/footer.blade.php b/resources/views/components/footer.blade.php old mode 100644 new mode 100755 diff --git a/resources/views/homepage.blade.php b/resources/views/homepage.blade.php index 258f4b96..6d92d0f5 100644 --- a/resources/views/homepage.blade.php +++ b/resources/views/homepage.blade.php @@ -7,6 +7,11 @@ {!! $base['page']['content']['main'] !!} + @if(!empty($components['catalog-1']['data'])) +
+ @include('components.catalog', ['data' => $components['catalog-1']['data'], 'component' => $components['catalog-1']['component'], 'class' => 'my-class']) +
+ @endif @if(!empty($articles['data']) || !empty($events))
@if(!empty($articles['data'])) diff --git a/styleguide/Repositories/HomepageRepository.php b/styleguide/Repositories/HomepageRepository.php index ff7c9b1b..13b89245 100644 --- a/styleguide/Repositories/HomepageRepository.php +++ b/styleguide/Repositories/HomepageRepository.php @@ -18,11 +18,21 @@ public function __construct(Factory $faker) /** * {@inheritdoc} */ - public function getHomepagePromos(int $page_id = null) + public function getHomepagePromos(array $data): array { - return [ - //'key' => app(\Factories\YourFactory::class)->create(5), - 'promos' => app(\Factories\GenericPromo::class)->create(5), + $promos = [ + 'homepageItems' => app(\Factories\GenericPromo::class)->create(5, false), + 'components' => [ + 'catalog-1' => [ + 'data' => app(\Factories\GenericPromo::class)->create(4, false), + 'component' => [ + 'filename' => 'catalog-1', + 'gradientOverlay' => true, + ], + ], + ], ]; + + return $promos; } } diff --git a/tests/Unit/Http/Controllers/HomepageControllerTest.php b/tests/Unit/Http/Controllers/HomepageControllerTest.php new file mode 100644 index 00000000..04b360c7 --- /dev/null +++ b/tests/Unit/Http/Controllers/HomepageControllerTest.php @@ -0,0 +1,71 @@ +getPageData(); + $request->data = $base; + + $promo_group_id = $this->faker->numberbetween(1, 3); + + // Create a fake data request + $data = app(Page::class)->create(1, true, [ + 'page' => [ + 'controller' => 'HomepageController', + ], + 'data' => [ + 'modular-catalog-1' => json_encode([ + 'id' => $promo_group_id, + 'config' => 'randomize|limit:2', + ]), + ], + ]); + + // Fake return + $return['promotions'] = app(GenericPromo::class)->create(3, false, [ + 'promo_group_id' => $promo_group_id, + 'group' => [ + 'promo_group_id' => $promo_group_id, + ], + ]); + + // Mock the connector and set the return + $wsuApi = Mockery::mock(Connector::class); + $wsuApi->shouldReceive('sendRequest')->with('cms.promotions.listing', Mockery::type('array'))->once()->andReturn($return); + + // Pass in the modular repository component data + // $modularComponents['modularComponents'] = app(ModularPageRepository::class, ['wsuApi' => $wsuApi])->getModularComponents($data); + + // Create a modular catalog component + $request->data['base']['data']['modular-catalog-1'] = json_encode(['id' => $promo_group_id]); + + // Construct the modular repository + $modularRepository = app(ModularPageRepository::class, ['wsuApi' => $wsuApi]); + + // Construct the homepage controller + $this->modularComponent = app(HomepageController::class, ['modularComponent' => $modularRepository]); + + // Create the view + $view = $this->modularComponent->index($request); + // $this->assertTrue(is_array($HomepageController)); + } +} diff --git a/tests/Unit/Http/Controllers/ProfileControllerTest.php b/tests/Unit/Http/Controllers/ProfileControllerTest.php index 873f09b3..3ac22beb 100644 --- a/tests/Unit/Http/Controllers/ProfileControllerTest.php +++ b/tests/Unit/Http/Controllers/ProfileControllerTest.php @@ -55,7 +55,7 @@ public function invalid_profile_should_404_using_profile_repository(): void // Construct the profile repository $profileRepository = app(ProfileRepository::class, ['wsuApi' => $wsuApi]); - // Construct the news controller + // Construct the profile controller $this->profileController = app(ProfileController::class, ['profile' => $profileRepository]); // Call the profile listing diff --git a/tests/Unit/Repositories/HomepageRepositoryTest.php b/tests/Unit/Repositories/HomepageRepositoryTest.php index de079fd2..ff06a0a2 100644 --- a/tests/Unit/Repositories/HomepageRepositoryTest.php +++ b/tests/Unit/Repositories/HomepageRepositoryTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\Attributes\Test; use App\Repositories\HomepageRepository; +use Factories\GenericPromo; use Tests\TestCase; use Mockery as Mockery; use Waynestate\Api\Connector; @@ -13,9 +14,11 @@ final class HomepageRepositoryTest extends TestCase #[Test] public function getting_homepage_promos_should_return_array(): void { + $promo_return = app(GenericPromo::class)->create(4, false); + // Fake return $return = [ - 'promotions' => [], + 'promotion' => $promo_return, ]; // Mock the connector and set the return @@ -23,7 +26,7 @@ public function getting_homepage_promos_should_return_array(): void $wsuApi->shouldReceive('sendRequest')->with('cms.promotions.listing', Mockery::type('array'))->once()->andReturn($return); // Get the promos - $promos = app(HomepageRepository::class, ['wsuApi' => $wsuApi])->getHomepagePromos($this->faker->randomDigit()); + $promos = app(HomepageRepository::class, ['wsuApi' => $wsuApi])->getHomepagePromos($promo_return); $this->assertTrue(is_array($promos)); }