Symfony API-Platform Assert\Choice does not work on setter - graphql

My app has a very standard user comment feature with also standard possibility for any visitor to mark comments as inappropriate.
In the resolution process for these marked comments the admin will decide which action to take, between no action, edit the comment or remove it.
The CommentReport entity includes Assert\Choice to allow only the 3 choices above.
The issue is that it is accepting any string.
On the Comment entity the set up is as follows:
....
* graphql={
* "withCustomArgsMutation"={
* "security"="is_granted('ROLE_ADMIN')",
* "mutation"=CommentReputationTypeResolver::class,
* "args"={
* "id"={"type"="ID!"},
* "resolutionType"={"type"="String!"},
* },
* "denormalization_context"={"groups"={"put-resolution"}},
* "normalization_context"={"groups"={"get"}}
* },
.....
/**
* #ORM\OneToMany(targetEntity=CommentReport::class, mappedBy="comment")
* #Groups({"get", "get-admin", "get-owner", "put-resolution"})
*/
private $commentReports;
Then the CommentReports entity:
class CommentReport
{
const RESOLUTION_TYPES = [
'no_action',
'edit',
'remove',
];
.....
/**
* #ORM\Column(type="string", length=10, nullable=true)
* #Assert\NotBlank(
* message="commentReport.resolutionType.notBlank",
* groups={"put-resolution"}
* )
* #Assert\Choice(
* choices=CommentReport::RESOLUTION_TYPES,
* message="commentReport.resolutionType.choice",
* groups={"put-resolution"}
* )
* #Groups({"get-admin", "put-resolution"})
*/
private $resolutionType;
Then on the DeserializeStage the request is intercepted to handle the setting of each individual CommentReport made for that Comment:
/Stage/DeserializeStage.php
if ($this->isAdmin($user) && $resourceClass === 'App\Entity\Comment' && $operationName === 'withCustomArgsMutation') {
// get all comment reports
$commentReports = $deserializeObject->getCommentReports();
foreach($commentReports as $report) {
$report->setResolutionType($context['args']['input']['resolutionType']);
}
}
All of this works but I can input any string and it will be accepted.
Of course, the expected result would be that Symfony would throw an exception for any string not on the RESOLUTION_TYPES array.
For info, this is the front-end mutation that triggers input:
`mutation putCommentReport(
$commentId: ID!
$resolutionType: String!
) {
withCustomArgsMutationComment(input: {
id: $commentId
resolutionType: $resolutionType
}){
comment{
id
}
}
}`;

As #Jeroen van der Laan pointed out, the validation group annotation was missing on the operation, which should be as follows;
App\Entity\Comment
* graphql={
* "withCustomArgsMutation"={
* "security"="is_granted('ROLE_ADMIN')",
* "mutation"=CommentReputationTypeResolver::class,
* "args"={
* "id"={"type"="ID!"},
* "resolutionType"={"type"="String!"},
* },
* "normalization_context"={"groups"={"get"}},
* "validation_groups"={"put-resolution"}
* },
Additionally, the Valid() assertion needs to be added to the CommentReport relation on Comment:
/**
* #ORM\OneToMany(targetEntity=CommentReport::class, mappedBy="comment")
* #Assert\Valid()
* #Groups({"get", "get-admin", "get-owner"})
*/
private $commentReports;

Related

Sorting does not work for the customized ProductListComponentService.(CustomProductListComponentService)

Spartacus lists 10 products in the pagination defualt on its product listing page. But I wanted it to be 12.
In my .ts file normally for PLP
protected productListComponentServiceDefault: ProductListComponentService,
I was using the structure.
but I created "CutomProductListComponentService" by customizing the "ProductListComponentService" file.
protected defaultPageSize = 12;
I did what I wanted by adding. Pagination artok 12 works.
But after this customization, the "SORTING" operation within the page does not work anymore.
Inside CutomProductListComponentService I added the following:
import { Injectable } from '#angular/core';
import { ActivatedRoute, Router } from '#angular/router';
import {
ActivatedRouterStateSnapshot,
CurrencyService,
LanguageService,
ProductSearchPage,
ProductSearchService,
RouterState,
RoutingService,
} from '#spartacus/core';
import { combineLatest, Observable, using } from 'rxjs';
import {
debounceTime,
distinctUntilChanged,
filter,
map,
shareReplay,
tap,
} from 'rxjs/operators';
// #ts-ignore
import { ProductListRouteParams, SearchCriteria } from './product-list.model';
/**
* The `ProductListComponentService` is used to search products. The service is used
* on the Product Listing Page, for listing products and the facet navigation.
*
* The service exposes the product search results based on the category and search
* route parameters. The route parameters are used to query products by the help of
* the `ProductSearchService`.
*/
#Injectable({ providedIn: 'root' })
export class CustomProductListComponentService {
// TODO: make it configurable
protected defaultPageSize = 12;
protected readonly RELEVANCE_ALLCATEGORIES = ':relevance:allCategories:';
constructor(
protected productSearchService: ProductSearchService,
protected routing: RoutingService,
protected activatedRoute: ActivatedRoute,
protected currencyService: CurrencyService,
protected languageService: LanguageService,
protected router: Router
) {}
/**
* Emits the search results for the current search query.
*
* The `searchResults$` is _not_ concerned with querying, it only observes the
* `productSearchService.getResults()`
*/
protected searchResults$: Observable<
ProductSearchPage
> = this.productSearchService
.getResults()
.pipe(filter((searchResult) => Object.keys(searchResult).length > 0));
/**
* Observes the route and performs a search on each route change.
*
* Context changes, such as language and currencies are also taken
* into account, so that the search is performed again.
*/
protected searchByRouting$: Observable<
ActivatedRouterStateSnapshot
> = combineLatest([
this.routing.getRouterState().pipe(
distinctUntilChanged((x, y) => {
// router emits new value also when the anticipated `nextState` changes
// but we want to perform search only when current url changes
return x.state.url === y.state.url;
})
),
...this.siteContext,
]).pipe(
debounceTime(0),
map(([routerState, ..._context]) => (routerState as RouterState).state),
tap((state: ActivatedRouterStateSnapshot) => {
const criteria = this.getCriteriaFromRoute(
state.params,
state.queryParams
);
this.search(criteria);
})
);
/**
* This stream is used for the Product Listing and Product Facets.
*
* It not only emits search results, but also performs a search on every change
* of the route (i.e. route params or query params).
*
* When a user leaves the PLP route, the PLP component unsubscribes from this stream
* so no longer the search is performed on route change.
*/
readonly model$: Observable<ProductSearchPage> = using(
() => this.searchByRouting$.subscribe(),
() => this.searchResults$
).pipe(shareReplay({ bufferSize: 1, refCount: true }));
/**
* Expose the `SearchCriteria`. The search criteria are driven by the route parameters.
*
* This search route configuration is not yet configurable
* (see https://github.com/SAP/spartacus/issues/7191).
*/
protected getCriteriaFromRoute(
routeParams: ProductListRouteParams,
queryParams: SearchCriteria
): SearchCriteria {
return {
query: queryParams.query || this.getQueryFromRouteParams(routeParams),
pageSize: queryParams.pageSize || this.defaultPageSize,
currentPage: queryParams.currentPage,
sortCode: queryParams.sortCode,
};
}
/**
* Resolves the search query from the given `ProductListRouteParams`.
*/
protected getQueryFromRouteParams({
query,
categoryCode,
brandCode,
}: ProductListRouteParams) {
if (query) {
return query;
}
if (categoryCode) {
return this.RELEVANCE_ALLCATEGORIES + categoryCode;
}
// TODO: drop support for brands as they should be treated
// similarly as any category.
if (brandCode) {
return this.RELEVANCE_ALLCATEGORIES + brandCode;
}
}
/**
* Performs a search based on the given search criteria.
*
* The search is delegated to the `ProductSearchService`.
*/
protected search(criteria: SearchCriteria): void {
const currentPage = criteria.currentPage;
const pageSize = criteria.pageSize;
const sort = criteria.sortCode;
this.productSearchService.search(
criteria.query,
// TODO: consider dropping this complex passing of cleaned object
Object.assign(
{},
currentPage && { currentPage },
pageSize && { pageSize },
sort && { sort }
)
);
}
/**
* Get items from a given page without using navigation
*/
getPageItems(pageNumber: number): void {
this.routing
.getRouterState()
.subscribe((route) => {
const routeCriteria = this.getCriteriaFromRoute(
route.state.params,
route.state.queryParams
);
const criteria = {
...routeCriteria,
currentPage: pageNumber,
};
this.search(criteria);
})
.unsubscribe();
}
/**
* Sort the search results by the given sort code.
*/
sort(sortCode: string): void {
this.route({ sortCode });
}
/**
* Routes to the next product listing page, using the given `queryParams`. The
* `queryParams` support sorting, pagination and querying.
*
* The `queryParams` are delegated to the Angular router `NavigationExtras`.
*/
protected route(queryParams: SearchCriteria): void {
this.router.navigate([], {
queryParams,
queryParamsHandling: 'merge',
relativeTo: this.activatedRoute,
});
}
/**
* The site context is used to update the search query in case of a
* changing context. The context will typically influence the search data.
*
* We keep this private for now, as we're likely refactoring this in the next
* major version.
*/
private get siteContext(): Observable<string>[] {
// TODO: we should refactor this so that custom context will be taken
// into account automatically. Ideally, we drop the specific context
// from the constructor, and query a ContextService for all contexts.
return [this.languageService.getActive(), this.currencyService.getActive()];
}
}
I call the Sort function in plp.ts as follows.
constructor(
protected productListComponentService: CustomProductListComponentService,
) {}
sortList(sortCode: string): void {
this.productListComponentService.sort(sortCode);
}
Sort process doesn't work. Can you help me? Thank you very much in advance.
You also need to replace "ProductListComponentService" to "CustomProductListComponentService" in "ProductFacetService".
The better way to do this is:
In providers of "ProductListModule", add:
{
provide: ProductListComponentService,
useClass: CustomProductListComponentService,
},

Cannot create async method which return ElementArrayFinder for E2E tests

I have a class where I want to make a async method which will return ElementArrayFinder. I need exactly this type, not ElementFinder[] because my waiters based on it and I can wait element which isn't present now in DOM. Below you can find a simple example of the structure. At row 'return this.collection;' i have an error:
[ts]
Type 'any[]' is not assignable to type 'ElementArrayFinder'.
Property 'browser_' is missing in type 'any[]'.
I tried to cast result in a different way and tried to use Promise.resolve, but no success. Could somebody help me with this case?
export class Test {
private grid1: ElementFinder;
private grid2: ElementFinder;
get collection1(): ElementArrayFinder {
return this.grid1
.element(by.css('tbody'))
.all(by.tagName('tr'));
}
get collection2(): ElementArrayFinder {
return this.grid2
.element(by.css('tbody'))
.all(by.tagName('tr'));
}
public async getCollection(): Promise<ElementArrayFinder> {
if (await this.collection.count() === 0) {
return this.collection1;
}
return this.collection2;
}
}
Bug report
Node Version: v8.2.1
Protractor Version: 5.2.0
I found the answer in file element.d.ts file.
/**
* Retrieve the elements represented by the ElementArrayFinder. The input
* function is passed to the resulting promise, which resolves to an
* array of ElementFinders.
*
* #alias element.all(locator).then(thenFunction)
* #view
* <ul class="items">
* <li>First</li>
* <li>Second</li>
* <li>Third</li>
* </ul>
*
* #example
* element.all(by.css('.items li')).then(function(arr) {
* expect(arr.length).toEqual(3);
* });
*
* // Or using the shortcut $$() notation instead of element.all(by.css()):
*
* $$('.items li').then(function(arr) {
* expect(arr.length).toEqual(3);
* });
*
* #param {function(Array.<ElementFinder>)} fn
* #param {function(Error)} errorFn
*
* #returns {!webdriver.promise.Promise} A promise which will resolve to
* an array of ElementFinders represented by the ElementArrayFinder.
*/
then<T>(fn?: (value: ElementFinder[] | any[]) => T | wdpromise.IThenable<T>, errorFn?: (error: any) => any): wdpromise.Promise<T>;
Based on this documentation ElementArrayFinder type is resolved by .then to the ElementFinder[]. That's why I cannot create method which return ElementArrayFinder type in promise, but can create method which simply return ElementArrayFinder type.

I cant log into phpMyAdmin

I have just installed Laragon on my new laptop. I have used Laragon before with no problems.
This time when I came to log in to phpMyAdmin I get "Cannot log in to the MySQL server" as an error.
I followed the instructions as before, and tried using Username:'root' and password:'' which is what it should be. No good, I have tried uninstalling and re-installing. Also no good.
This is my config.inc.php
Any ideas anyone. I have tried a few other configs but to no avail
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* phpMyAdmin sample configuration, you can use it as base for
* manual configuration. For easier setup you can use setup/
*
* All directives are explained in documentation in the doc/ folder
* or at <http://docs.phpmyadmin.net/>.
*
* #package PhpMyAdmin
*/
/*
* This is needed for cookie based authentication to encrypt password in
* cookie
*/
$cfg['blowfish_secret'] = ''; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
/*
* Servers configuration
*/
$i = 0;
/*
* First server
*/
$i++;
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['AllowNoPassword'] = true;
$cfg['Servers'][$i]['port'] = 3306;
/*
* Laragon: set phpmyadmin to not timeout so quickly
*/
$cfg['LoginCookieValidity'] = 72000;
/*
* phpMyAdmin configuration storage settings.
*/
/* User used to manipulate with storage */
// $cfg['Servers'][$i]['controlhost'] = '';
// $cfg['Servers'][$i]['controlport'] = '';
// $cfg['Servers'][$i]['controluser'] = 'pma';
// $cfg['Servers'][$i]['controlpass'] = 'pmapass';
/* Storage database and tables */
// $cfg['Servers'][$i]['pmadb'] = 'phpmyadmin';
// $cfg['Servers'][$i]['bookmarktable'] = 'pma__bookmark';
// $cfg['Servers'][$i]['relation'] = 'pma__relation';
// $cfg['Servers'][$i]['table_info'] = 'pma__table_info';
// $cfg['Servers'][$i]['table_coords'] = 'pma__table_coords';
// $cfg['Servers'][$i]['pdf_pages'] = 'pma__pdf_pages';
// $cfg['Servers'][$i]['column_info'] = 'pma__column_info';
// $cfg['Servers'][$i]['history'] = 'pma__history';
// $cfg['Servers'][$i]['table_uiprefs'] = 'pma__table_uiprefs';
// $cfg['Servers'][$i]['tracking'] = 'pma__tracking';
// $cfg['Servers'][$i]['userconfig'] = 'pma__userconfig';
// $cfg['Servers'][$i]['recent'] = 'pma__recent';
// $cfg['Servers'][$i]['favorite'] = 'pma__favorite';
// $cfg['Servers'][$i]['users'] = 'pma__users';
// $cfg['Servers'][$i]['usergroups'] = 'pma__usergroups';
// $cfg['Servers'][$i]['navigationhiding'] = 'pma__navigationhiding';
// $cfg['Servers'][$i]['savedsearches'] = 'pma__savedsearches';
// $cfg['Servers'][$i]['central_columns'] = 'pma__central_columns';
/* Contrib / Swekey authentication */
// $cfg['Servers'][$i]['auth_swekey_config'] = '/etc/swekey-pma.conf';
/*
* End of servers configuration
*/
/*
* Directories for saving/loading files from server
*/
$cfg['UploadDir'] = '';
$cfg['SaveDir'] = '';
/**
* Whether to display icons or text or both icons and text in table row
* action segment. Value can be either of 'icons', 'text' or 'both'.
*/
//$cfg['RowActionType'] = 'both';
/**
* Defines whether a user should be displayed a "show all (records)"
* button in browse mode or not.
* default = false
*/
//$cfg['ShowAll'] = true;
/**
* Number of rows displayed when browsing a result set. If the result
* set contains more rows, "Previous" and "Next".
* default = 30
*/
//$cfg['MaxRows'] = 50;
/**
* disallow editing of binary fields
* valid values are:
* false allow editing
* 'blob' allow editing except for BLOB fields
* 'noblob' disallow editing except for BLOB fields
* 'all' disallow editing
* default = blob
*/
//$cfg['ProtectBinary'] = 'false';
/**
* Default language to use, if not browser-defined or user-defined
* (you find all languages in the locale folder)
* uncomment the desired line:
* default = 'en'
*/
//$cfg['DefaultLang'] = 'en';
//$cfg['DefaultLang'] = 'de';
/**
* default display direction (horizontal|vertical|horizontalflipped)
*/
//$cfg['DefaultDisplay'] = 'vertical';
/**
* How many columns should be used for table display of a database?
* (a value larger than 1 results in some information being hidden)
* default = 1
*/
//$cfg['PropertiesNumColumns'] = 2;
/**
* Set to true if you want DB-based query history.If false, this utilizes
* JS-routines to display query history (lost by window close)
*
* This requires configuration storage enabled, see above.
* default = false
*/
//$cfg['QueryHistoryDB'] = true;
/**
* When using DB-based query history, how many entries should be kept?
*
* default = 25
*/
//$cfg['QueryHistoryMax'] = 100;
/**
* Should error reporting be enabled for JavaScript errors
*
* default = 'ask'
*/
//$cfg['SendErrorReports'] = 'ask';
/*
* You can find more configuration options in the documentation
* in the doc/ folder or at <http://docs.phpmyadmin.net/>.
*/
?>
Any help please
change this line:
$cfg['Servers'][$i]['host'] = 'localhost';
to this:
$cfg['Servers'][$i]['host'] = '127.0.0.1';
and consider using a password for the root user anyway. I discourage using no password for mysql.

Java Optional why not an ifNotPresent method?

I was wondering why on the Java8 API the Optional class have the method ifPresent(Consumer< T> consumer) and not the ifNotPresent(Runnable** consumer)?
What's the intent of the API? Isn't it to emulate the functional pattern matching?
** Java doesn't have a zero argument void functional interface...
As Misha said, this feature will come with jdk9 in the form of a ifPresentOrElse method.
/**
* If a value is present, performs the given action with the value,
* otherwise performs the given empty-based action.
*
* #param action the action to be performed, if a value is present
* #param emptyAction the empty-based action to be performed, if no value is
* present
* #throws NullPointerException if a value is present and the given action
* is {#code null}, or no value is present and the given empty-based
* action is {#code null}.
* #since 9
*/
public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) {
if (value != null) {
action.accept(value);
} else {
emptyAction.run();
}
}

Is there a function like _compile_select or get_compiled_select()?

Looks like _compile_select is deprecated and get_compiled_select is not added to 2.1.0. Are there any other functions like those two? And also I am curious. Is there any particular reason to not adding get_compiled_select() to Active Record and removing _compile_select?
I've added get_compiled_select() to DB_active_rec.php and it seems to work without problem, but i wouldn't remove _compile_select() since it's used in many other methods.
The pull request for adding this method is here, with some other useful methods like:
get_compiled_select()
get_compiled_insert()
get_compiled_update()
get_compiled_delete()
https://github.com/EllisLab/CodeIgniter/pull/307
if you want just the method, it's just this:
/**
* Get SELECT query string
*
* Compiles a SELECT query string and returns the sql.
*
* #access public
* #param string the table name to select from (optional)
* #param boolean TRUE: resets AR values; FALSE: leave AR vaules alone
* #return string
*/
public function get_compiled_select($table = '', $reset = TRUE)
{
if ($table != '')
{
$this->_track_aliases($table);
$this->from($table);
}
$select = $this->_compile_select();
if ($reset === TRUE)
{
$this->_reset_select();
}
return $select;
}

Resources